I have an array like this:
arr = [1,3,4,5,7,8]
I need to check if the next number is consecutive to the current number and, if it is, store both together otherwise i store the single number in an array.
The above array should give the following result:
newArray = [1, 345, 78]
I tried a simple solution and only achieve what i wanted if was only 2 consecutive numbers:
for (i = 0; i < array.length; i++) {
if (array[i] === array[i - 1] + 1) {
newArray.push(array[i - 1] + array[i]);
} else {
newArray.push(array[i]);
}
}
And when i tried to do for more numbers things got very messy.
I know this doesnt look like a major problem, but i am trying this for hours and cant find the result.