-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdo_while_loop.js
85 lines (77 loc) · 1.51 KB
/
do_while_loop.js
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
let x = 0;
do {
x = x + 1;
} while (x < 3);
console.assertEqual(x, 3);
let y = 0;
do {
y = y + 1;
} while (false);
console.assertEqual(y, 1);
let i = 3;
let counter = 1;
do {
i = i - 1;
counter *= 2;
counter += 1;
} while (i !== 0);
console.assertEqual(i, 0);
console.assertEqual(counter, 15);
do {
break;
console.assertNotReached();
} while (console.assertNotReached());
let breakFlag = false;
let breakCounter = 0;
do {
breakCounter += 1;
if (breakFlag) {
break;
}
breakFlag = true;
} while (true);
console.assertEqual(breakFlag, true);
console.assertEqual(breakCounter, 2);
let z = 0;
do {
z += 1;
if (z >= 3) {
break;
}
continue;
console.assertNotReached();
} while (console.assertNotReached());
console.assertEqual(z, 3);
let bcIdx = 10;
let bcCount = 0;
do {
bcIdx -= 1;
if (bcIdx >= 7) {
continue;
}
if (bcIdx === 2) {
break;
}
bcCount += 1;
} while (true);
console.assertEqual(bcIdx, 2);
console.assertEqual(bcCount, 4);
function returnInsideWhileLoop() {
let riwlCounter = 3;
do {
return riwlCounter;
} while (riwlCounter -= 1);
console.assertNotReached();
}
console.assertEqual(returnInsideWhileLoop(), 3);
function returnInsideWhileLoop2() {
let riwl2Counter = 0;
do {
if (riwl2Counter >= 10) {
return riwl2Counter;
}
riwl2Counter += 1;
} while (true);
console.assertNotReached();
}
console.assertEqual(returnInsideWhileLoop2(), 10);