Loading workspace environment...
Automate repetitive tasks efficiently using for and while loops.
for (let i = 0; i < 5; i++) { ... }Counts from 0 up to 4, running the block 5 times.while (condition) { ... }Repeats block as long as condition remains true.break;Immediately exits the current loop.console.log("Rocket Launch Countdown:");
for (let i = 5; i >= 1; i--) {
console.log("T-minus " + i + " seconds...");
}
console.log("BLAST OFF! 🚀");Rocket Launch Countdown:
T-minus 5 seconds...
T-minus 4 seconds...
T-minus 3 seconds...
T-minus 2 seconds...
T-minus 1 seconds...
BLAST OFF! 🚀let i = 5;Initializes counter at 5.i >= 1;Runs as long as counter is at least 1.i--Decrements counter by 1 after each cycle.let count = 0;
while (count < 5) {
console.log(count);
// Forgot count++! Infinite loop hangs the browser!
}let count = 0;
while (count < 5) {
console.log(count);
count++; // Advances state towards false
}Architectural Explanation: Always ensure your loop updates the variable tested in its condition so it eventually terminates.
for (let i = 0; i < 3; i++) {
console.log("Hello");
}Use a for loop to calculate the sum of all numbers from 1 to 10. Print the final sum (should be 55).