Consider:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] // even length
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] // odd length
For the even length we can start at index 0 which will end at index 8.
For the odd length we can start at index 1 which will end at index 9.
In both cases we never touch the last element.
If n
is our array’s length,
n = arr.length
our starting position can be computed to be,
start = n % 2
Now we have a starting point,
for (let i = start; i < n; i += 2) {
}
That’s one way, at least. Most people work backwards through the array but I like this way, better.