-
Notifications
You must be signed in to change notification settings - Fork 0
/
loops.js
74 lines (62 loc) · 887 Bytes
/
loops.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
// Traditional for loop
for (let i = 1; i < 5; i++) {
console.log(i * 3);
}
/* Output:
3
6
9
12
*/
// for-of loop: iterate over values in an object
let animals = ["dog", "cat", "horse", "pig"];
for (let animal of animals) {
if (animal.length === 3) {
console.log(animal);
}
}
/* Output:
dog
cat
pig
*/
// for-in loop: iterate over properties of an object
let myCar = {
make: "Honda",
model: "Civic",
year: 1996,
color: "black"
}
for (let prop in myCar) {
console.log(`${prop}: ${myCar[prop]}`);
}
/* Output:
make: "Honda"
model: "Civic"
year: 1996
color: "black"
*/
// while loop
let x = 1;
while (x < 5) {
console.log(x);
x++;
}
/* Output:
1
2
3
4
*/
// do-while loop
let y = 1;
do {
console.log(y);
y++;
} while (y < 5)
/* Output:
1
2
3
4
*/