javascript loop alternating from beginning and end code example
Example: javascript loop alternating from beginning and end
let arr = Array.from(Array(10).keys());
// arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for (let i = (arr.length - 1), j = 0; i - j >= 0; i--, j++) {
console.log(arr[i]);
console.log(arr[j]);
}
// output:
// 9
// 0
// 8
// 1
// 7
// 2
// 6
// 3
// 5
// 4