-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-array-loop.js
More file actions
35 lines (35 loc) · 883 Bytes
/
test-array-loop.js
File metadata and controls
35 lines (35 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function test(arr) {
let N = arr.length;
let i = Math.floor(N / 2);
let j = i + 1;
let isLeft = true,isLoopBreak=false;
while (i >= 0 || j < N) {
if(isLoopBreak){
console.log("I", i+1, "j", j)
console.log("[i]", arr[i+1], "arr[j]", arr[j])
isLoopBreak=false;
}
console.log("I", i, "j", j)
console.log("[i]", arr[i], "arr[j]", arr[j])
if (i == 0 && j == N - 1) {
break;
}
if (isLeft) {
if (i) {
i--;
}
if (j != N - 1) {
isLeft = false;
}
} else {
if (j != N - 1) {
j++;
isLoopBreak=true;
}
if (i) {
isLeft = true;
}
}
}
}
console.log(test([1, 2, 2, 6, 9, 4,7]));