0
0
Javascriptprogramming~20 mins

For loop in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
For Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A"0-1-2-"
B"0-1-2"
C"1-2-3-"
D"0-1-2-3-"
Attempts:
2 left
💡 Hint
Remember the loop runs while i is less than 3, starting from 0.
Predict Output
intermediate
2: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);
A0
B6
C10
D3
Attempts:
2 left
💡 Hint
The loop stops when i equals 3.
Predict Output
advanced
2: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());
A"11 12 13 21 22 23"
B"11 21 12 22 13 23"
C"123 123"
D"1 2 3 1 2 3"
Attempts:
2 left
💡 Hint
The outer loop runs twice, inner loop runs three times each.
Predict Output
advanced
2: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);
A"024"
B"1234"
C"135"
D"0246"
Attempts:
2 left
💡 Hint
Continue skips even numbers.
Predict Output
expert
3: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);
}
A3 3 3
B0 1 2
C0 0 0
D1 2 3
Attempts:
2 left
💡 Hint
The variable i is declared with let inside the loop.