Challenge - 5 Problems
Do–while Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple do–while loop
What is the output of this JavaScript code?
Javascript
let count = 0; do { console.log(count); count++; } while (count < 3);
Attempts:
2 left
💡 Hint
Remember, do–while runs the block first, then checks the condition.
✗ Incorrect
The loop starts with count = 0, prints it, then increments. It repeats while count < 3, so it prints 0, 1, and 2.
❓ Predict Output
intermediate2:00remaining
Do–while loop with break statement
What will this code print to the console?
Javascript
let i = 5; do { console.log(i); if (i === 3) break; i--; } while (i > 0);
Attempts:
2 left
💡 Hint
The break stops the loop when i equals 3.
✗ Incorrect
The loop prints 5, then 4, then 3. When i is 3, break stops the loop immediately.
🧠 Conceptual
advanced1:30remaining
Understanding do–while loop execution
Which statement about do–while loops is true?
Attempts:
2 left
💡 Hint
Think about when the condition is checked in a do–while loop.
✗ Incorrect
In a do–while loop, the code inside the loop runs first, then the condition is checked. So it always runs at least once.
❓ Predict Output
advanced2:30remaining
Output with nested do–while loops
What is the output of this nested do–while loop code?
Javascript
let a = 1; do { let b = 1; do { console.log(a * b); b++; } while (b <= 2); a++; } while (a <= 2);
Attempts:
2 left
💡 Hint
Trace the values of a and b carefully through both loops.
✗ Incorrect
Outer loop runs for a=1 and a=2. Inner loop runs for b=1 and b=2 each time. Outputs: 1*1=1, 1*2=2, 2*1=2, 2*2=4.
🔧 Debug
expert2:00remaining
Identify the error in this do–while loop
What error will this code cause when run?
Javascript
let x = 0; do { console.log(x); x++ } while x < 3;
Attempts:
2 left
💡 Hint
Check the syntax of the while condition in a do–while loop.
✗ Incorrect
The while condition must be inside parentheses. Missing parentheses causes SyntaxError.