-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwhile_loop.js
More file actions
94 lines (85 loc) · 1.65 KB
/
while_loop.js
File metadata and controls
94 lines (85 loc) · 1.65 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
let x = 0;
while (x < 3) {
x = x + 1;
}
console.assertEqual(x, 3);
let y = 0;
while (false) {
y = y + 1;
}
console.assertEqual(y, 0);
let i = 3;
let counter = 1;
while (i !== 0) {
i = i - 1;
counter *= 2;
counter += 1;
}
console.assertEqual(i, 0);
console.assertEqual(counter, 15);
while (true) {
break;
console.assertNotReached();
}
let breakFlag = false;
let breakCounter = 0;
while (true) {
breakCounter += 1;
if (breakFlag) {
break;
}
breakFlag = true;
}
console.assertEqual(breakFlag, true);
console.assertEqual(breakCounter, 2);
let z = 0;
while (z < 3) {
z += 1;
continue;
console.assertNotReached();
}
console.assertEqual(z, 3);
let continueIdx = 0;
let continueCounter = 0;
while (continueIdx < 10) {
continueIdx += 1;
if (continueCounter === 3) {
continue;
}
continueCounter += 1;
}
console.assertEqual(continueIdx, 10);
console.assertEqual(continueCounter, 3);
let bcIdx = 10;
let bcCount = 0;
while (true) {
bcIdx -= 1;
if (bcIdx >= 7) {
continue;
}
if (bcIdx === 2) {
break;
}
bcCount += 1;
}
console.assertEqual(bcIdx, 2);
console.assertEqual(bcCount, 4);
function returnInsideWhileLoop() {
let riwlCounter = 3;
while (riwlCounter -= 1) {
return riwlCounter;
}
console.assertNotReached();
}
console.assertEqual(returnInsideWhileLoop(), 2);
function returnInsideWhileLoop2() {
let riwl2Counter = 0;
while (true) {
if (riwl2Counter >= 10) {
return riwl2Counter;
}
riwl2Counter += 1;
}
console.assertNotReached();
}
console.assertEqual(returnInsideWhileLoop2(), 10);