javascript loop alternating from beginning and end

Solutions on MaxInterview for javascript loop alternating from beginning and end by the best coders in the world

showing results for - "javascript loop alternating from beginning and end"
Brandon
18 Apr 2020
1let arr = Array.from(Array(10).keys());
2// arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3
4for (let i = (arr.length - 1), j = 0; i - j >= 0; i--, j++) {
5    console.log(arr[i]);
6    console.log(arr[j]);
7}
8// output:
9// 9
10// 0
11// 8
12// 1
13// 7
14// 2
15// 6
16// 3
17// 5
18// 4