-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_while_loop.js
More file actions
38 lines (34 loc) · 1.14 KB
/
08_while_loop.js
File metadata and controls
38 lines (34 loc) · 1.14 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
// while loop is used when you don't know how many cycles you need. for example: while not gameover === true
for (let i = 0; i <= 5; i++) {
console.log(i);
}
//Recreating the above for loop w/ a while loop:
let j = 0;
while (j <= 5) {
console.log(j);
j++;
}
// Pick a target number we are trying to guess
const target = Math.floor(Math.random() * 10);
// Make initial guess
let guess = Math.floor(Math.random() * 10);
// Continue looping while guess is not the target num
while (guess !== target) {
console.log(`Target: ${target} Guess: ${guess}`);
// IMPORTANT!!
// Update the value of guess each time through the loop
guess = Math.floor(Math.random() * 10);
}
console.log(`Target: ${target} Guess: ${guess}`);
console.log(`CONGRATS YOU WIN!!`);
// ========= Break Intro =========
const target = Math.floor(Math.random() * 10);
let guess;
//ANOTHER APPROACH TO THE PREVIOUS GUESSING 'GAME'
while (true) {
if (target === guess) break; //Break stops the loop in its tracks
console.log(`Target: ${target} Guess: ${guess}`);
guess = Math.floor(Math.random() * 10);
}
console.log(`Target: ${target} Guess: ${guess}`);
console.log(`CONGRATS YOU WIN!!`);