Challenge - 5 Problems
For Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple for loop
What will be the output of the following code?
Javascript
let result = ''; for (let i = 0; i < 3; i++) { result += i + '-'; } console.log(result);
Attempts:
2 left
💡 Hint
Remember the loop runs while i is less than 3, starting from 0.
✗ Incorrect
The loop runs with i values 0, 1, and 2. Each time it adds i and a dash to the string. So the final string is "0-1-2-".
❓ Predict Output
intermediate2:00remaining
For loop with break statement
What will be printed by this code?
Javascript
let sum = 0; for (let i = 1; i <= 5; i++) { if (i === 3) break; sum += i; } console.log(sum);
Attempts:
2 left
💡 Hint
The loop stops when i equals 3.
✗ Incorrect
The loop adds 1 and 2 to sum, then stops before adding 3. So sum is 1 + 2 = 3.
❓ Predict Output
advanced2:30remaining
Nested for loops output
What is the output of this nested loop code?
Javascript
let output = ''; for (let i = 1; i <= 2; i++) { for (let j = 1; j <= 3; j++) { output += `${i}${j} `; } } console.log(output.trim());
Attempts:
2 left
💡 Hint
The outer loop runs twice, inner loop runs three times each.
✗ Incorrect
For i=1, j runs 1 to 3 producing 11 12 13. For i=2, j runs 1 to 3 producing 21 22 23. Combined output is "11 12 13 21 22 23".
❓ Predict Output
advanced2:00remaining
For loop with continue statement
What will this code print?
Javascript
let text = ''; for (let i = 0; i <= 5; i++) { if (i % 2 === 0) continue; text += i; } console.log(text);
Attempts:
2 left
💡 Hint
Continue skips even numbers.
✗ Incorrect
The loop skips i when it is even (0,2,4). It adds only odd numbers 1, 3, and 5 to text, so output is "135".
❓ Predict Output
expert3:00remaining
For loop with asynchronous behavior
What will be logged to the console when this code runs?
Javascript
for (let i = 0; i < 3; i++) { setTimeout(() => console.log(i), 0); }
Attempts:
2 left
💡 Hint
The variable i is declared with let inside the loop.
✗ Incorrect
Using let creates a new binding for each loop iteration, so the logged values are 0, 1, and 2 as expected.