Bird
Raised Fist0
Javascriptprogramming~20 mins

For loop in Javascript - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1.

What does a for loop do in JavaScript?

easy
A. Changes the value of a variable once
B. Stops the program immediately
C. Repeats a block of code a set number of times
D. Creates a new variable

Solution

  1. Step 1: Understand the purpose of a for loop

    A for loop is designed to repeat code multiple times based on a condition.
  2. Step 2: Compare options with the for loop behavior

    Only Repeats a block of code a set number of times correctly describes repeating code multiple times.
  3. Final Answer:

    Repeats a block of code a set number of times -> Option C
  4. Quick Check:

    For loop = repeat code [OK]
Hint: For loops repeat code multiple times [OK]
Common Mistakes:
  • Thinking for loop stops program
  • Confusing variable creation with looping
  • Assuming for loop runs only once
2.

Which of the following is the correct syntax for a basic for loop in JavaScript?

for (___; ___; ___) {
// code
}
easy
A. for (i = 0; i < 5; i++)
B. for i = 0; i < 5; i++
C. for (i < 5; i = 0; i++)
D. for (i = 0; i++; i < 5)

Solution

  1. Step 1: Recall the for loop syntax

    The correct syntax is: for (start; condition; change) { ... }
  2. Step 2: Check each option against syntax

    for (i = 0; i < 5; i++) matches the correct syntax with initialization, condition, and increment.
  3. Final Answer:

    for (i = 0; i < 5; i++) -> Option A
  4. Quick Check:

    Syntax = for (init; cond; change) [OK]
Hint: For loop needs three parts inside parentheses [OK]
Common Mistakes:
  • Missing parentheses around for loop parts
  • Wrong order of parts inside for loop
  • Using semicolons incorrectly
3.

What will be the output of this code?

let result = '';
for (let i = 1; i <= 3; i++) {
  result += i + ' ';
}
console.log(result.trim());
medium
A. "3 2 1"
B. "1 2 3"
C. "123"
D. "1 2 3 4"

Solution

  1. Step 1: Understand the loop iterations

    The loop runs with i = 1, 2, 3 because i <= 3.
  2. Step 2: Track the string concatenation

    Each iteration adds the number and a space to result: "1 ", then "1 2 ", then "1 2 3 ". trim() removes the last space.
  3. Final Answer:

    "1 2 3" -> Option B
  4. Quick Check:

    Loop 1 to 3 adds numbers with spaces [OK]
Hint: Check loop range and string building carefully [OK]
Common Mistakes:
  • Ignoring the spaces added
  • Forgetting trim() removes trailing space
  • Assuming loop includes 4
4.

Find the error in this for loop code:

for (let i = 0; i < 5; i) {
  console.log(i);
}
medium
A. Missing curly braces around loop body
B. Incorrect variable declaration 'let i = 0'
C. Wrong loop condition 'i < 5'
D. Missing increment operator 'i++' in the loop

Solution

  1. Step 1: Check the loop increment part

    The loop has 'i' instead of 'i++' which means i never changes.
  2. Step 2: Understand the effect of missing increment

    Without increment, the loop runs forever or does not progress, causing an error or infinite loop.
  3. Final Answer:

    Missing increment operator 'i++' in the loop -> Option D
  4. Quick Check:

    Increment needed to change loop variable [OK]
Hint: Check all three parts of for loop syntax carefully [OK]
Common Mistakes:
  • Forgetting to increment loop variable
  • Using wrong condition syntax
  • Missing braces but allowed in single line
5.

You want to create a for loop that prints only even numbers from 2 to 10 inclusive. Which code snippet does this correctly?

hard
A. for (let i = 2; i <= 10; i += 2) { console.log(i); }
B. for (let i = 0; i <= 10; i++) { if (i % 2 === 0) console.log(i); }
C. for (let i = 2; i < 10; i++) { if (i % 2 === 0) console.log(i); }
D. for (let i = 2; i <= 10; i++) { if (i % 2 === 1) console.log(i); }

Solution

  1. Step 1: Identify the range and condition for even numbers

    We want numbers 2, 4, 6, 8, 10 inclusive.
  2. Step 2: Check each option for correctness

    for (let i = 2; i <= 10; i += 2) { console.log(i); } increments by 2 starting at 2, printing only even numbers up to 10. for (let i = 0; i <= 10; i++) { if (i % 2 === 0) console.log(i); } starts from 0 and prints 0 which is outside the 2-10 range. for (let i = 2; i < 10; i++) { if (i % 2 === 0) console.log(i); } misses 10 because it uses i < 10. for (let i = 2; i <= 10; i++) { if (i % 2 === 1) console.log(i); } prints odd numbers, which is wrong.
  3. Final Answer:

    for (let i = 2; i <= 10; i += 2) { console.log(i); } -> Option A
  4. Quick Check:

    Increment by 2 prints evens directly [OK]
Hint: Use i += 2 to skip odd numbers [OK]
Common Mistakes:
  • Using wrong loop condition (i < 10)
  • Printing odd numbers by mistake
  • Not incrementing properly to skip odds
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard