Challenge - 5 Problems
Loop Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested loops with break
What is the output of the following JavaScript code?
let result = '';
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
if (j === 2) break;
result += i + j + ' ';
}
}
console.log(result.trim());Javascript
let result = ''; for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 3; j++) { if (j === 2) break; result += i + j + ' '; } } console.log(result.trim());
Attempts:
2 left
💡 Hint
Remember that the inner loop breaks when j equals 2, so only j=1 runs each time.
✗ Incorrect
The inner loop runs only once per outer loop iteration because it breaks when j === 2. So for each i, j=1 runs, adding i+j to result. i=1: 1+1=2, i=2: 2+1=3, i=3: 3+1=4. So result is "2 3 4".
❓ Predict Output
intermediate2:00remaining
Loop with continue statement output
What will be printed by this code?
let output = '';
for (let k = 0; k < 5; k++) {
if (k % 2 === 0) continue;
output += k + ',';
}
console.log(output.slice(0, -1));Javascript
let output = ''; for (let k = 0; k < 5; k++) { if (k % 2 === 0) continue; output += k + ','; } console.log(output.slice(0, -1));
Attempts:
2 left
💡 Hint
The continue skips even numbers, so only odd k values are added.
✗ Incorrect
The loop runs k from 0 to 4. When k is even (0,2,4), continue skips adding. So only k=1 and k=3 are added, resulting in "1,3".
❓ Predict Output
advanced2:00remaining
Output of labeled break in nested loops
What is the output of this code?
let res = '';
outer: for (let a = 1; a <= 3; a++) {
for (let b = 1; b <= 3; b++) {
if (a * b > 3) break outer;
res += a + b + ' ';
}
}
console.log(res.trim());Javascript
let res = ''; outer: for (let a = 1; a <= 3; a++) { for (let b = 1; b <= 3; b++) { if (a * b > 3) break outer; res += a + b + ' '; } } console.log(res.trim());
Attempts:
2 left
💡 Hint
The labeled break stops both loops when a*b > 3.
✗ Incorrect
The loops add a+b to res until a*b > 3. Iterations: a=1,b=1(1), add 2; a=1,b=2(2), add 3; a=1,b=3(3), add 4; a=2,b=1(2), add 3; a=2,b=2(4 > 3 triggers break). So res is "2 3 4 3".
❓ Predict Output
advanced2:00remaining
Final value after loop with post-increment
What is the value of variable x after this code runs?
let x = 0;
for (let i = 0; i < 5; i++) {
x += i++;
}
console.log(x);Javascript
let x = 0; for (let i = 0; i < 5; i++) { x += i++; } console.log(x);
Attempts:
2 left
💡 Hint
Remember that i++ increases i after its value is used in the addition.
✗ Incorrect
Loop iterations: i=0, x+=0, i becomes 1, then i++ in for loop makes i=2; i=2, x+=2, i=3, then i=4; i=4, x+=4, i=5, loop ends. x=0+2+4=6.
🧠 Conceptual
expert2:00remaining
Understanding loop execution with async/await
Consider this code snippet:
Which statement best describes the output behavior?
async function test() {
for (let i = 0; i < 3; i++) {
await new Promise(resolve => setTimeout(resolve, 100));
console.log(i);
}
}
test();Which statement best describes the output behavior?
Javascript
async function test() { for (let i = 0; i < 3; i++) { await new Promise(resolve => setTimeout(resolve, 100)); console.log(i); } } test();
Attempts:
2 left
💡 Hint
Await pauses the loop until the promise resolves before continuing.
✗ Incorrect
The await inside the for loop pauses each iteration until the 100ms timeout resolves, so logs happen one by one with 100ms delay each.