-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path020-while-loop.js
More file actions
98 lines (72 loc) · 2.14 KB
/
020-while-loop.js
File metadata and controls
98 lines (72 loc) · 2.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
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
// 1: WHAT IS A WHILE LOOP?
// while (condition) {
// // code to be executed
// }
// let count = 1;
// while (count <= 5) {
// console.log("Count is: " + count);
// count++;
// }
// Count is: 1
// Count is: 2
// Count is: 3
// Count is: 4
// Count is: 5
// 2: WHILE VS FOR LOOP
// let password = "";
// while (password !== "javascript123") {
// password = prompt("Enter the password:");
// }
// console.log("Access granted!");
// 3: COMMON PATTERNS & EXAMPLES
// Example 1: Reading through data until a condition is met
// let numbers = [3, 7, 2, 9, 1, 5];
// let index = 0;
// while (index < numbers.length && numbers[index] !== 9) {
// console.log("Current number: " + numbers[index]);
// index++;
// }
// console.log("Found 9 at index: " + index);
// Example 2: Countdown timer
// let seconds = 5;
// while (seconds > 0) {
// console.log("Time remaining: " + seconds + " seconds");
// seconds--;
// }
// console.log("Time's up!");
// Example 3: Processing until a flag changes
// let dataProcessed = false;
// let attempts = 0;
// while (!dataProcessed && attempts < 10) {
// console.log("Processing attempt: " + (attempts + 1));
// // Simulate some processing
// if (Math.random() > 0.7) {
// dataProcessed = true;
// console.log("Data processed successfully!");
// }
// attempts++;
// }
// if (!dataProcessed) {
// console.log("Failed to process data after 10 attempts");
// }
// 4: COMMON PITFALLS
// let i = 1;
// while (i <= 5) {
// console.log(i);
// // Oops! Forgot to increment i
// }
// let x = 0;
// while (x = 5) { // Should be x !== 5 or x < 5
// console.log(x);
// x++;
// }
// 5: PRACTICAL EXERCISE
let randomNumber = 0;
let attempts = 0;
while (randomNumber !== 7) {
randomNumber = Math.floor(Math.random() * 10) + 1;
attempts++;
console.log("Attempt " + attempts + ": Generated " + randomNumber);
}
console.log("Finally got 7 after " + attempts + " attempts!");
// 6: CONCLUSION