forked from AustinCodingAcademy/javascript-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop.js
More file actions
58 lines (47 loc) · 1.67 KB
/
loop.js
File metadata and controls
58 lines (47 loc) · 1.67 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
// for loop
const cars = ["ford", "chevrolet", "dodge", "mazda", "fiat"];
for (let i = 0; i < cars.length; i++) {
console.log(cars[i])
}
// for...in loop
const persons = {
firstName: "Jane",
lastName: "Doe",
birthDate: "Jan 5, 1925",
gender: "female",
}
// Use a for...in loop to console.log each key.
var text = "";
var x = Object.keys(persons);
for (x in persons) {
text += x + " ";
}
console.log(text);
// Use a for...in loop and if state to console.log the value associated with the key birthDate.
var text1 = "";
var x = Object.keys(persons);
for (x in persons) {
if (x === 'birthDate') {
text1 += persons[x];
}
}
console.log(text1);
// while loop
let number = 0;
while (number < 1000){
number ++;
console.log(number);
}
//do while
let digit = 0;
do {
digit += 1;
console.log(digit);
} while (digit < 1000);
// When is a for loop better than a while loop?
// A for loop runs "n" times but a while loop runs until a certain condition becomes false. If the initial condition is false, the while loop wont run at all. So if your condition is initially false, a for loop is better.
// What is the difference between a for loop and a for...in loop?
// A for loop repeats until a specified condition evaluates to false.
//The for...in statement iterates a specified variable over all the enumerable properties of an object. For each distinct property, JavaScript executes the specified statements.
// What is the difference between a while loop and a do...while loop?
// The do...while statement repeats until a specified condition evaluates to false. A while statement executes its statements as long as a specified condition evaluates to true.