Trying to identify consecutive numbers from an array of numbers has been my latest coding challenge. Here's the snippet I've come up with:
let arr = [4, 3, 5, 4, 3];
let new_arr = [];
for (let i = 0; i < arr.length; i++) {
let internalArr = [arr[i]];
new_arr.push(internalArr);
if (arr[i] - arr[i + 1] === 1) {
new_arr.push([arr[i], arr[i + 1]]);
}
}
console.log(new_arr);
I'm struggling with getting more than two elements into the new array. For instance, [5, 4, 3]
should also be included in the new_arr
. However, currently, I'm only able to form arrays with two elements. Any insights on what might be missing?