I have arrays like this one below.
[1,2,3, 10, 15, 24,25,26,27]
What I want is to filter out the non consecutive numbers excluding the first number in a series but keeping the last in a series like this below.
[2,3, 25,26,27]
I am new to coding and I'm not sure how to write this code yet but I have mocked up pseudocode, would you check this and see if my logic is on track?
Var startArray = [1,2,3, 10, 15, 24,25,26,27];
Var endArray = [ ];
Loop: while there are numbers left in the start array
GET: The first number of the startArray
IF: the first number is 1
MOVE: to next number in the array
IF: the current number + 1 in the array subtracted by the current number -1 in the array === 2
PUSH: current number into endArray
ELSE IF: the current number -1 ===1
PUSH: current number into endArray
END IF
END LOOP
Thank you for your help.
You will just have to loop over everything and simply check, if the current number is one higher than the previous number. If so, push it to your resulting array:
var startArray = [1, 2, 3, 10, 15, 24, 25, 26, 27];
var endArray = [];
for(var i = 0; i < startArray.length; i++) {
if(startArray[i] - 1 === startArray[i - 1]) {
endArray.push(startArray[i]);
}
}
$.writeln(endArray); // => 2, 3, 25, 26, 27