-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloops_mini_exercise.html
More file actions
116 lines (76 loc) · 3.19 KB
/
loops_mini_exercise.html
File metadata and controls
116 lines (76 loc) · 3.19 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>My Loops Mini Exercises page</title>
</head>
<body>
</body>
<script>
(function () { // IIFE
// Code goes here!
// TODO: Create these While Loops
// 1. Create a while loop that counts to 10 by 2’s
// A. It will increment the count by 2 for each loop
// B. It will log the value of count to the console on each loop.
/* var count = 0; // inital value
while (count <= 10) {
// logic
count += 2;
console.log('count', count) //final value on loop
}
// 2. Create a while loop that runs until isComplete is equal to true
// A. isComplete should be changed to false after the 20th iteration of the loop.
// B. You will need to create a variable to track your loops progress (incrementor)
// C. Log each iteration and the value of isComplete
var isComplete = true;
var i = 0;
while(isComplete) {
//your logic
console.log(i + ': ' + isComplete)
if (i >= 20) isComplete = false;
i++;
}
*/
// TODO: Do-While Loops
// 1. Create a do-while loop that counts to 10 by 2’s
// A. It will increment the count by 2 for each loop
// B. It will log the value of count to the console on each loop.
/*
incrementor = 0;
do {
// your logic
console.log(incrementor + ' Going up!')
incrementor += 2;
} while(incrementor <= 10);
*/
// 2. Create a do-while loop that console logs the number if it's odd.
// A. Create a variable called number and increment it by one on each run of the loop.
// B. Only print out the value if it's odd.
// C. End the loop when the number reaches 50.
// TODO: For LOOPs
// 1. Create a for loop that goes to 100 from 0.
// A. It will print out the number if it's even.
// B. It will print out the number if it's divisible by five evenly.
// C. If it's both A and B then it should print out the string "BOTH" instead of the number for that iteration.
for (var i = 0; i <= 100; i++) {
var isDivisibleBy2 = (i % 2 === 0); //even
var isDivisibleBy5 = (i % 5 === 0); //odd
if (isDivisibleBy2 && isDivisibleBy5)
console.log( i + ' both')
if (isDivisibleBy2 && !isDivisibleBy5) console.log(i)
if (isDivisibleBy5 && !isDivisibleBy2) console.log(i)
}
// 2. Create a for loop that runs 50 times with an increment variable starting at zero.
// A. Create a variable called total to store the total value of the incrementer.
// B. Log the current total on each loop.
// C. Log the sum of all the incrementer values (0+1+2+3….) after the loop has completed.
var total = 0;
for (var i = 0; i <50; i++) {
total += i;
console.log('sub total:', total)
}
console.log('total:', total)
})();
</script>
</html>