for (let i = 1; i <= 30; i++) {
console.log(`Student ${i}`);
}
Key Insight: Computers excel at repetitive tasks - they're fast and never get tired!
Today's Learning Objectives π―
π Master for and while loops
π¦ Store multiple values in arrays
π Access and modify array elements
β‘ Use powerful array methods: map, filter, reduce
π§ͺ Introduction to Test-Driven Development (TDD)
πͺ Practice with real coding challenges
While Loops β°
Keep going while a condition is true
let count = 0;
while (count < 5) {
console.log(`Count is: ${count}`);
count++; // Don't forget this!
}
β οΈ Watch Out for Infinite Loops!
Always ensure your loop condition will eventually become false!
Quick Challenge:
What happens if we forget count++? π€
For Loops π’
When you know how many times to repeat
// for (initialization; condition; update)
for (let i = 0; i < 5; i++) {
console.log(`Iteration ${i}`);
}
Parts of a For Loop:
Initialization:let i = 0
Condition:i < 5
Update:i++
Execution Order:
Initialize once
Check condition
Execute body
Update
Go to step 2
Loop Control: Break & Continue π
break
Exits the loop completely
for (let i = 0; i < 10; i++) {
if (i === 5) break;
console.log(i);
} // Output: 0, 1, 2, 3, 4
continue
Skips to next iteration
for (let i = 0; i < 5; i++) {
if (i === 2) continue;
console.log(i);
} // Output: 0, 1, 3, 4
Quick Loop Practice π»
5-Minute Challenge: Create These Loops
Count from 10 to 1 (countdown)
Print only even numbers from 0 to 20
Calculate the sum of numbers 1 to 100
Hint for #3:
let sum = 0;
for (let i = 1; i <= 100; i++) { // What goes here?
}
π Historical Moment: Y2K π
When Loop Boundaries Mattered Most
The Y2K Panic of 1999
The Clinton administration called preparing for Y2K "the single largest technology management challenge in history."
Feared Consequences:
β‘ Power grid failures
π₯ Medical equipment crashes
π¦ Banking system collapses
βοΈ Transportation shutdowns
The Core Problem:
Systems stored years as 2 digits (99 for 1999). When 2000 came, computers would read "00" - creating invalid loop conditions and date calculations!
Source: NPR - "Y2K seems like a joke now, but in 1999 people were really freaking out"
Y2K: Why It Matters to You π
The Power of Proper Loop Boundaries
The Y2K Bug in Code:
// Buggy Y2K-style code
let year = 99; // Meant to be 1999
year++; // Now it's 00 - is that 1900 or 2000?
// Loop that breaks with wrong boundaries
for (let year = 95; year <= 05; year++) {
console.log(`Processing year: 19${year}`);
} // What happens when year = 00? π₯
π΄ The acronym that emerged: TEOTWAWKI
"The End Of The World As We Know It"
π Reflection Question:
Why must programmers think carefully about loop boundaries and edge cases?
Think about: array indexing, date ranges, off-by-one errors...