Challenge - 5 Problems
Loop Control Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this loop with break?
Consider the following JavaScript code. What will it print to the console?
Javascript
for (let i = 0; i < 5; i++) { if (i === 3) { break; } console.log(i); }
Attempts:
2 left
💡 Hint
The break statement stops the loop immediately when the condition is met.
✗ Incorrect
The loop starts from 0 and prints each number. When i equals 3, the break stops the loop, so 3 and 4 are not printed.
❓ Predict Output
intermediate2:00remaining
What does continue do in this loop?
Look at this code. What numbers will be printed?
Javascript
for (let i = 0; i < 5; i++) { if (i === 2) { continue; } console.log(i); }
Attempts:
2 left
💡 Hint
The continue statement skips the current loop iteration when the condition is true.
✗ Incorrect
When i is 2, continue skips the console.log for that iteration, so 2 is not printed.
🧠 Conceptual
advanced1:30remaining
Why is loop control important?
Why do we need loop control statements like break and continue in loops?
Attempts:
2 left
💡 Hint
Think about how loops can be controlled to avoid problems or to skip some steps.
✗ Incorrect
Loop control statements help manage how loops run, allowing us to stop early or skip certain steps, which is useful for efficiency and correctness.
❓ Predict Output
advanced2:00remaining
What happens without loop control in this code?
What will happen when this code runs?
Javascript
let i = 0; while (i < 5) { console.log(i); // missing i++ here }
Attempts:
2 left
💡 Hint
Think about what happens if the loop variable never changes.
✗ Incorrect
Since i never increases, the condition i < 5 is always true, so the loop never ends and keeps printing 0.
❓ Predict Output
expert2:30remaining
What is the output with nested loops and break?
What will this code print to the console?
Javascript
for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (j === 1) { break; } console.log(`i=${i}, j=${j}`); } }
Attempts:
2 left
💡 Hint
The break stops the inner loop when j equals 1, so only j=0 prints each time.
✗ Incorrect
For each i, the inner loop runs j from 0 to 2, but breaks when j is 1, so only j=0 is printed per i.