Bird
Raised Fist0
Javascriptprogramming~20 mins

Loop execution flow in Javascript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Loop Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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());
A"2 3 4"
B"2 3 4 5 6 7"
C"2 3 4 5 6"
D"2 4 6"
Attempts:
2 left
💡 Hint
Remember that the inner loop breaks when j equals 2, so only j=1 runs each time.
❓ Predict Output
intermediate
2: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));
A"1,3"
B"1,3,4"
C"1,3,5"
D"1,3,5,7"
Attempts:
2 left
💡 Hint
The continue skips even numbers, so only odd k values are added.
❓ Predict Output
advanced
2: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());
A"2 3 3"
B"2 3"
C"2 3 4 3"
D"2 3 4 5"
Attempts:
2 left
💡 Hint
The labeled break stops both loops when a*b > 3.
❓ Predict Output
advanced
2: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);
A10
B6
C8
D12
Attempts:
2 left
💡 Hint
Remember that i++ increases i after its value is used in the addition.
🧠 Conceptual
expert
2:00remaining
Understanding loop execution with async/await
Consider this code snippet:
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();
AThrows a syntax error because await cannot be used in for loops.
BLogs 0, 1, 2 immediately without delay.
CLogs 0, 1, 2 all after approximately 300ms at once.
DLogs 0, 1, 2 with 100ms delay between each log sequentially.
Attempts:
2 left
💡 Hint
Await pauses the loop until the promise resolves before continuing.

Practice

(1/5)
1. What does a for loop do in JavaScript?
easy
A. Stops the program immediately
B. Runs code only once
C. Creates a new variable
D. Repeats code a set number of times

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: Identify the correct description

    Among the options, only repeating code a set number of times matches the for loop behavior.
  3. Final Answer:

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

    for loop = repeat fixed times [OK]
Hint: For loops repeat code a fixed number of times [OK]
Common Mistakes:
  • Thinking for loops run only once
  • Confusing for loops with if statements
  • Believing for loops stop the program
2. Which of the following is the correct syntax for a for loop in JavaScript?
easy
A. for i = 0 to 5 { }
B. for (let i = 0; i < 5; i++) { }
C. loop (i = 0; i < 5; i++) { }
D. for (i < 5; i = 0; i++) { }

Solution

  1. Step 1: Recall JavaScript for loop syntax

    The correct syntax uses for (initialization; condition; increment) { }.
  2. Step 2: Compare options to syntax

    Only for (let i = 0; i < 5; i++) { } matches the correct syntax with let i = 0; i < 5; i++.
  3. Final Answer:

    for (let i = 0; i < 5; i++) { } -> Option B
  4. Quick Check:

    for loop syntax = initialization; condition; increment [OK]
Hint: Remember for loop uses three parts inside parentheses [OK]
Common Mistakes:
  • Using incorrect keywords like 'loop'
  • Swapping initialization and condition
  • Missing semicolons inside for parentheses
3. What will be the output of this code?
let sum = 0;
for (let i = 1; i <= 3; i++) {
  sum += i;
}
console.log(sum);
medium
A. 6
B. 3
C. 0
D. Error

Solution

  1. Step 1: Trace the loop iterations

    The loop runs with i = 1, 2, 3. Each time, sum adds i: 0+1=1, 1+2=3, 3+3=6.
  2. Step 2: Determine final sum value

    After the loop, sum is 6, which is printed by console.log.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Sum 1+2+3 = 6 [OK]
Hint: Add numbers step-by-step inside the loop [OK]
Common Mistakes:
  • Stopping loop early
  • Adding wrong values
  • Confusing sum initialization
4. Find the error in this loop:
for (let i = 0; i < 5; i--) {
  console.log(i);
}
medium
A. Console.log is incorrect
B. Syntax error in for loop
C. The loop will run infinitely because i decreases
D. Variable i is not declared

Solution

  1. Step 1: Analyze the loop increment/decrement

    The loop uses i--, so i decreases each time starting from 0.
  2. Step 2: Check loop condition

    The condition is i < 5, which is true for all i less than 5, including negative numbers. Since i decreases, it never reaches 5 or more, so loop never ends.
  3. Final Answer:

    The loop will run infinitely because i decreases -> Option C
  4. Quick Check:

    Decrement with < condition causes infinite loop [OK]
Hint: Check if increment matches loop condition direction [OK]
Common Mistakes:
  • Confusing i-- with i++
  • Ignoring loop condition logic
  • Assuming loop stops automatically
5. How many times will this loop run?
let count = 0;
let i = 10;
while (i > 0) {
  i -= 3;
  count++;
}
console.log(count);
hard
A. 4
B. 3
C. 5
D. 6

Solution

  1. Step 1: Trace the while loop iterations

    Start i=10, count=0. Each loop: i decreases by 3, count increases by 1.
    Iterations:
    1) i=7, count=1
    2) i=4, count=2
    3) i=1, count=3
    4) i=-2, count=4 (loop stops after this because i > 0 is false)
  2. Step 2: Count total iterations

    The loop runs 4 times before i becomes -2 and condition fails.
  3. Final Answer:

    4 -> Option A
  4. Quick Check:

    Subtract 3 until i ≤ 0 counts 4 times [OK]
Hint: Count how many times i decreases by 3 until ≤ 0 [OK]
Common Mistakes:
  • Counting one less or more iteration
  • Not updating count correctly
  • Misunderstanding while loop condition
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