0
0
Javascriptprogramming~20 mins

Do–while loop in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Do–while Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A1 2 3
B0 1 2
C0 1 2 3
D1 2
Attempts:
2 left
💡 Hint
Remember, do–while runs the block first, then checks the condition.
Predict Output
intermediate
2: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);
A5 4 3
B5 4 3 2 1
C5 4
D3 2 1
Attempts:
2 left
💡 Hint
The break stops the loop when i equals 3.
🧠 Conceptual
advanced
1:30remaining
Understanding do–while loop execution
Which statement about do–while loops is true?
ADo–while loops cannot be used to run code multiple times.
BThe loop body may not execute if the condition is false at the start.
CThe condition is checked before the loop body runs.
DThe loop body executes at least once, even if the condition is false initially.
Attempts:
2 left
💡 Hint
Think about when the condition is checked in a do–while loop.
Predict Output
advanced
2: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);
A1 2 2 4
B1 2 1 2
C2 4 2 4
D1 2 3 4
Attempts:
2 left
💡 Hint
Trace the values of a and b carefully through both loops.
🔧 Debug
expert
2: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;
AReferenceError
BTypeError
CSyntaxError
DNo error, outputs 0 1 2
Attempts:
2 left
💡 Hint
Check the syntax of the while condition in a do–while loop.