const arr = [0, 1, 2, 3, 4, 5, 6, 7];
const n = arr.length;
// Middle index for even-sized arrays
const mid = Math.floor(n / 2);
// Left starts before the middle, right starts at the middle
let left = mid - 1;
let right = mid;
// Iterate outward from the middle
while (left >= 0 || right < n) {
if (right < n) {
console.log(arr[right]);
right++;
}
if (left >= 0) {
console.log(arr[left]);
left--;
}
}
/*
run:
4
3
5
2
6
1
7
0
*/