Challenge - 5 Problems
Break 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 code using break in a loop?
Look at the code below. What will it print to the console?
Javascript
let result = ''; for (let i = 0; i < 5; i++) { if (i === 3) { break; } result += i; } console.log(result);
Attempts:
2 left
💡 Hint
Remember, break stops the loop immediately when the condition is met.
✗ Incorrect
The loop adds numbers 0, 1, and 2 to the string. When i becomes 3, the break stops the loop before adding 3.
❓ Predict Output
intermediate2:00remaining
What happens when break is inside a nested loop?
What will this code print to the console?
Javascript
let output = ''; for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (j === 1) { break; } output += `${i}${j} `; } } console.log(output.trim());
Attempts:
2 left
💡 Hint
Break only stops the inner loop, not the outer one.
✗ Incorrect
The inner loop breaks when j equals 1, so only j=0 is added for each i. The outer loop continues for i=0,1,2.
❓ Predict Output
advanced2:00remaining
What is the output when break is used inside a switch statement?
Check the code below. What will it print?
Javascript
const value = 2; switch (value) { case 1: console.log('One'); break; case 2: console.log('Two'); break; case 3: console.log('Three'); break; default: console.log('Default'); }
Attempts:
2 left
💡 Hint
Remember that break stops the switch from continuing to the next cases.
✗ Incorrect
Since value is 2, it matches case 2 and prints 'Two'. The break stops further cases from running.
❓ Predict Output
advanced2:00remaining
What error does this code raise?
What error will this code cause when run?
Javascript
while (true) { if (false) { break; } console.log('Looping'); break; break; }
Attempts:
2 left
💡 Hint
Multiple break statements in a row are allowed but some may be unreachable.
✗ Incorrect
The code prints 'Looping' once and then breaks out of the loop. No syntax or runtime error occurs.
🧠 Conceptual
expert2:00remaining
Which option correctly explains break behavior in nested loops?
In JavaScript, what does the break statement do inside nested loops?
Attempts:
2 left
💡 Hint
Think about which loop the break is inside.
✗ Incorrect
Break stops the closest loop it is inside. Outer loops continue unless they also have a break.