-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop_lecture.html
More file actions
308 lines (216 loc) · 5.93 KB
/
loop_lecture.html
File metadata and controls
308 lines (216 loc) · 5.93 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Loops Lecture</title>
</head>
<body>
<h1>Loops Lecture</h1>
<script>
"use strict";
// ================================ WHILE LOOP
/*
While loop syntax:
while (conditionIsTrue) {
do something...
}
Used to repeat a block of code until a condition changes to break the loop
Likely used when the number of iterations is unknown
Diagram ***Curriculum***
*/
// basic example
let x = 1;
while(x < 10){
console.log(x);
++x;
}
//
// console.log(1);
// console.log(2);
// console.log(3);
// possible use cases
while(gameRunning) {
drawCanvas();
}
while(timerOn) { // elsewhere: a button when clicked, sets timer to off
countUp();
}
let password = "Open Sesame";
let guess = "";
while(guess !== password) {
guess = prompt("Please enter the password.");
}
// condition never executes
let x = 10;
while (x < 5) {
console.log(x);
x--;
}
// infinitely executes
let x = 5;
while (x !== 4) {
x++;
}
// given variable x, while x is less than 100, increment x by 5. x begins at 0. Console log the value of x for each iteration;
let x = 0;
while (x < 100) {
console.log(x);
x += 5;
}
// TODO - write a while loop that prints your name 10 times
let x = 1;
while (x <= 10){
console.log('Rick');
x++;
}
/*
TODO CHALLENGE - write a function, getNum1To10 that returns a number between 1 and 10 from the user.
Keep prompting them until a valid number is given
*/
function getNum1to10(){
let num = 0;
while (num < 1 || num > 10) {
num = prompt('Please enter a number between 1 and 10');
}
return num;
}
alert(getNum1to10(10));
// ================================ DO-WHILE LOOP
/*
While loop syntax:
do {
do something no matter what...
} while(conditionIsTrue);
Used to execute a block and code at least once
and repeat it until the condition is no longer true
Diagram ***Curriculum***
*/
// basic example
let x = 10;
do {
alert("This will run.");
} while (x !== 10);
// use case
let password = "";
do {
password = prompt("Please enter a valid password.");
}
while(password !== 'letmein');
// The do-while executes at least once
/*
TODO - write a do-while loop that will alert the current date and time and
continue checking with the user if they want to see the latest time.
*/
let dateQuestion = false;
do {
dateQuestion = confirm('Display the current date and time?');
if (dateQuestion === true) {
alert(new Date().toLocaleString());
}
} while (dateQuestion === false);
// ================================ FOR LOOP
/*
For loop diagram ***Curriculum***
For loop syntax:
for (initialization; test; iteration) {
do something a set number of times
}
Used to perform an action based on a number of iterations
*/
// with incrementer
for (let i = 1; i < 10; ++i) {
console.log(i);
}
// with decrementer
for (let i = 20; i >= 10; --i) {
console.log(i);
}
// nested for loops
for (let i = 0; i <= 10; ++i) {
console.log("Badgers, badgers, badgers");
for (let j = 0; j <= 1; ++j) {
console.log("Mushroom");
}
}
// multiple iterators (less common)
for (let i = 0, j = 9; i < 10; ++i, --j) {
console.log("value of i:" + i + " and value of j: " + j);
}
// console.log each letter in a given string
// get length of string
// let someString = prompt("Enter a word");
//
// for (let i = 0; i <= someString.length; i += 1) {
// console.log(someString.charAt(i));
// }
//TODO - write a for loop that prints all even numbers from 2 to 100
for (let i = 2; i <= 100; i += 2) {
console.log(i);
}
//TODO - write a for loop that prints all integers from 20 down to 1
for (let i = 20; i >= 1; --i) {
console.log(i);
}
// ================================ BREAK / CONTINUE
// break - keyword to end the execution of a loop
let y = 0;
while (true) {
if (y === 10) {
console.log("End of loop!");
break;
}
++y;
console.log(y);
}
// web example
let password = "123";
while (true) {
password = prompt("Please enter a valid password: ");
if (password === "123") {
break;
}
}
// console.log("Access granted.");
// game example
let gameRunning = true;
let lives = 3;
while (gameRunning) {
// play game
if (lives === 0) {
console.log("Game over");
break;
}
}
// output values up to 50 from 0, if value is 25, stop the loop
let x = 0;
while (x < 50) {
if (x === 25) {
break;
}
console.log(x);
x += 1;
}
// continue
console.log("Continue example");
let y = 0;
while (true) {
y++;
if (y === 100) {
console.log("End of loop!");
break;
}
if (y % 5 === 0) {
continue;
}
console.log(y);
}
// web example
/*
Display a stylized list of weather info by year
in which a specific geographic region experienced drought conditions.
Skip any years that do not meet the criteria.
If the years have already been displayed in the list, continue to the next year.
*/
</script>
</body>
</html>