-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path024-infinity-loop.js
More file actions
141 lines (118 loc) · 3.58 KB
/
024-infinity-loop.js
File metadata and controls
141 lines (118 loc) · 3.58 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
// 1: WHAT IS AN INFINITE LOOP?
// while (true) {
// console.log("This will run forever!");
// }
// let i = 0;
// while (i < 10) {
// console.log(i);
// // Oops! We forgot to increment i
// }
// // 2: WHEN ARE INFINITE LOOPS USEFUL?
// while (true) {
// updateGameState();
// renderGraphics();
// checkUserInput();
// }
// 3: LOOP CONTROL TECHNIQUES
// 1. Using Break Properly
// let count = 0;
// while (true) {
// console.log("Count:", count);
// count++;
// if (count >= 5) {
// break; // Exit when we reach 5
// }
// }
// console.log("Loop finished!");
// 2. Using Continue for Flow Control
// for (let i = 0; i < 10; i++) {
// if (i % 2 === 0) {
// continue; // Skip even numbers
// }
// console.log(i); // Only odd numbers are printed
// }
// 3. Flag Variables for Control
// let shouldContinue = true;
// let attempts = 0;
// while (shouldContinue) {
// attempts++;
// console.log("Attempt:", attempts);
// // Complex condition
// if (attempts > 10 || Math.random() > 0.9) {
// shouldContinue = false;
// }
// }
// 4. Labels for Nested Loop Control
outerLoop: for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
console.log(`i: ${i}, j: ${j}`);
if (i === 1 && j === 1) {
break outerLoop; // Breaks out of BOTH loops
}
}
}
console.log("Done!");
// 4: COMMON PITFALLS & DEBUGGING
// Pitfall #1: Forgetting to Update the Loop Variable
// let i = 0;
// while (i < 10) {
// console.log(i);
// // Missing: i++
// }
// Pitfall #2: Wrong Comparison Operator
// let i = 10;
// while (i > 0) {
// console.log(i);
// i++; // Should be i-- to approach 0
// }
// Pitfall #3: Condition Never Becomes False
// let x = 0;
// while (x != 10) {
// console.log(x);
// x += 2; // Skips 10! Goes 8, 10, 12...
// }
// let safety = 0;
// let i = 0;
// while (i < 10) {
// console.log(i);
// // Your logic here
// safety++;
// if (safety > 1000) {
// console.error("Safety limit reached!");
// break;
// }
// }
// 5: PRACTICAL EXAMPLES
// Example 1: User Input Validation
// let validInput = false;
// let attempts = 0;
// const MAX_ATTEMPTS = 3;
// while (!validInput && attempts < MAX_ATTEMPTS) {
// let userInput = prompt("Enter a number between 1-10:");
// let num = parseInt(userInput);
// if (num >= 1 && num <= 10) {
// console.log("Valid input:", num);
// validInput = true;
// } else {
// attempts++;
// console.log(`Invalid! Attempts remaining: ${MAX_ATTEMPTS - attempts}`);
// }
// }
// if (!validInput) {
// console.log("Maximum attempts reached!");
// }
// Example 2: Finding First Match in Nested Data
let products = [
[{ name: "Laptop", price: 1000 }, { name: "Mouse", price: 20 }],
[{ name: "Keyboard", price: 50 }, { name: "Monitor", price: 300 }],
];
let found = false;
searchLoop: for (let i = 0; i < products.length; i++) {
for (let j = 0; j < products[i].length; j++) {
if (products[i][j].price < 50) {
console.log("Affordable product found:", products[i][j].name);
found = true;
break searchLoop;
}
}
}