Challenge - 5 Problems
Labeled Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested loops with labeled break
What is the output of this JavaScript code using a labeled break?
Javascript
outerLoop: for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 3; j++) { if (i * j > 3) { break outerLoop; } console.log(`${i},${j}`); } }
Attempts:
2 left
💡 Hint
Remember that labeled break exits the entire outer loop immediately.
✗ Incorrect
The code prints pairs until i*j > 3. When i=2 and j=3, 2*3=6 > 3, so break outerLoop stops all loops. The last printed pair is 2,2.
❓ Predict Output
intermediate2:00remaining
Output of nested loops with labeled continue
What is the output of this JavaScript code using a labeled continue?
Javascript
outer: for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 3; j++) { if (j === 2) { continue outer; } console.log(`${i},${j}`); } }
Attempts:
2 left
💡 Hint
Labeled continue skips to the next iteration of the outer loop.
✗ Incorrect
When j equals 2, continue outer skips the rest of the inner loop and goes to the next i. So only j=1 is printed for each i.
🔧 Debug
advanced2:00remaining
Identify the error with labeled continue usage
What error does this JavaScript code produce?
Javascript
outer: for (let i = 0; i < 2; i++) { for (let j = 0; j < 2; j++) { if (j === 1) { continue inner; } console.log(i, j); } }
Attempts:
2 left
💡 Hint
Check if the label used in continue exists.
✗ Incorrect
The label 'inner' is not defined anywhere, so using 'continue inner;' causes a ReferenceError.
❓ Predict Output
advanced2:00remaining
Output with nested labeled break and continue
What is the output of this JavaScript code?
Javascript
outer: for (let i = 1; i <= 3; i++) { inner: for (let j = 1; j <= 3; j++) { if (i === j) { continue outer; } if (i + j === 4) { break inner; } console.log(`${i},${j}`); } }
Attempts:
2 left
💡 Hint
Remember continue outer skips to next i, break inner exits inner loop.
✗ Incorrect
When i===j, continue outer skips to next i. When i+j===4, break inner exits inner loop. The printed pairs are 1,2 then next i=2, j=1 then i=3, j=1 and j=2.
🧠 Conceptual
expert2:00remaining
Effect of labeled break on loop execution
Consider this code snippet:
What is the value of variable
label1: for (let a = 0; a < 2; a++) {
label2: for (let b = 0; b < 2; b++) {
if (a === b) {
break label1;
}
console.log(a, b);
}
}What is the value of variable
a after the loops finish?Attempts:
2 left
💡 Hint
Labeled break exits the outer loop immediately when condition is met.
✗ Incorrect
The break label1 stops the outer loop when a === b (which is true at a=0,b=0). The outer loop ends immediately, so a remains 0 after the loops finish.