0
0
Javascriptprogramming~20 mins

Why loop control is required in Javascript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Loop Control Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
}
A0 1 2
B0 1 2 3 4
C3 4
D0 1 2 3
Attempts:
2 left
💡 Hint
The break statement stops the loop immediately when the condition is met.
Predict Output
intermediate
2: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);
}
A0 1 3 4
B0 1 2 3 4
C0 1 2 3
D2
Attempts:
2 left
💡 Hint
The continue statement skips the current loop iteration when the condition is true.
🧠 Conceptual
advanced
1:30remaining
Why is loop control important?
Why do we need loop control statements like break and continue in loops?
ATo make the loop run forever without stopping.
BTo stop or skip parts of the loop based on conditions, preventing infinite loops or unwanted actions.
CTo automatically increase the loop counter by 2 each time.
DTo print all values without any condition.
Attempts:
2 left
💡 Hint
Think about how loops can be controlled to avoid problems or to skip some steps.
Predict Output
advanced
2: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
}
AIt prints 0 to 4 and stops.
BIt throws a syntax error.
CIt prints 0 forever (infinite loop).
DIt prints nothing.
Attempts:
2 left
💡 Hint
Think about what happens if the loop variable never changes.
Predict Output
expert
2: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}`);
  }
}
A
i=0, j=0
i=0, j=1
i=1, j=0
i=2, j=0
B
i=0, j=0
i=0, j=1
i=1, j=0
i=1, j=1
i=2, j=0
i=2, j=1
C
i=0, j=0
i=0, j=1
i=0, j=2
i=1, j=0
i=1, j=1
i=1, j=2
i=2, j=0
i=2, j=1
i=2, j=2
D
i=0, j=0
i=1, j=0
i=2, j=0
Attempts:
2 left
💡 Hint
The break stops the inner loop when j equals 1, so only j=0 prints each time.