Bird
Raised Fist0
Javascriptprogramming~20 mins

Nested loops in Javascript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Nested Loops Master
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 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"1-1 1-2 1-3 2-1 2-2 2-3 3-1 3-2 3-3"
B"1-1 1-2 2-1 2-2 3-1 3-2"
C"1-1 2-1 3-1"
D"1-1 2-1 3-1 3-2"
Attempts:
2 left
💡 Hint
Remember that the inner loop breaks when j equals 2, so it only adds the first pair for each i.
❓ Predict Output
intermediate
2:00remaining
Counting iterations in nested loops
How many times will the inner loop run in this code?
let count = 0;
for (let i = 0; i < 4; i++) {
  for (let j = 0; j < i; j++) {
    count++;
  }
}
console.log(count);
Javascript
let count = 0;
for (let i = 0; i < 4; i++) {
  for (let j = 0; j < i; j++) {
    count++;
  }
}
console.log(count);
A10
B6
C4
D12
Attempts:
2 left
💡 Hint
Count how many times j runs for each i: when i=0, j runs 0 times; i=1, j runs 1 time; etc.
❓ Predict Output
advanced
2:00remaining
Output of nested loops with continue and labels
What is the output of this code?
let output = '';
outer: for (let i = 1; i <= 3; i++) {
  for (let j = 1; j <= 3; j++) {
    if (i === j) continue outer;
    output += i + j + ' ';
  }
}
console.log(output.trim());
Javascript
let output = '';
outer: for (let i = 1; i <= 3; i++) {
  for (let j = 1; j <= 3; j++) {
    if (i === j) continue outer;
    output += i + j + ' ';
  }
}
console.log(output.trim());
A"12 13 23 31 32"
B"12 13 21 23 31 32 33"
C"12 13 21 23 31 32"
D"21 31 32"
Attempts:
2 left
💡 Hint
The continue with label skips the rest of the outer loop iteration when i equals j.
❓ Predict Output
advanced
2:00remaining
Nested loops with array modification
What is the output of this code?
const arr = [1, 2, 3];
let result = [];
for (let i = 0; i < arr.length; i++) {
  for (let j = i; j < arr.length; j++) {
    result.push(arr[i] + arr[j]);
  }
}
console.log(result.join(','));
Javascript
const arr = [1, 2, 3];
let result = [];
for (let i = 0; i < arr.length; i++) {
  for (let j = i; j < arr.length; j++) {
    result.push(arr[i] + arr[j]);
  }
}
console.log(result.join(','));
A"2,3,4,4,5,6"
B"2,3,4,3,4,5"
C"2,3,4,5,6,7"
D"1,3,5,2,4,6"
Attempts:
2 left
💡 Hint
Add arr[i] + arr[j] starting with j = i, so pairs include sums of same and later elements.
🧠 Conceptual
expert
2:00remaining
Time complexity of nested loops
Consider this code:
for (let i = 1; i <= n; i++) {
  for (let j = 1; j <= i; j++) {
    // constant time operation
  }
}

What is the time complexity in Big O notation?
AO(n^2)
BO(log n)
CO(n log n)
DO(n)
Attempts:
2 left
💡 Hint
Sum the inner loop counts: 1 + 2 + 3 + ... + n.

Practice

(1/5)
1. What does a nested loop in JavaScript mean?
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 2; j++) {
console.log(i, j);
}
}
easy
A. A loop inside another loop, running multiple times
B. A loop that runs only once
C. A loop that runs backwards
D. A loop that skips some numbers

Solution

  1. Step 1: Understand the code structure

    The code has one loop inside another, called nested loops.
  2. Step 2: Explain the behavior

    The inner loop runs fully for each iteration of the outer loop, so it runs multiple times.
  3. Final Answer:

    A loop inside another loop, running multiple times -> Option A
  4. Quick Check:

    Nested loop = loop inside loop [OK]
Hint: Nested means one loop inside another [OK]
Common Mistakes:
  • Thinking nested loops run only once
  • Confusing nested loops with single loops
  • Believing nested loops skip numbers
2. Which of the following is the correct syntax for a nested loop in JavaScript?
easy
A. for (let i = 0; i < 3; i++) { for (let j = 0; j < 2; j++) { } }
B. for i in range(3) { for j in range(2) { } }
C. while (i < 3) { while (j < 2) }
D. loop i from 0 to 3 { loop j from 0 to 2 { } }

Solution

  1. Step 1: Identify JavaScript loop syntax

    JavaScript uses for (let variable = start; condition; increment) syntax.
  2. Step 2: Check nested loops syntax

    for (let i = 0; i < 3; i++) { for (let j = 0; j < 2; j++) { } } correctly nests two for loops with proper syntax and braces.
  3. Final Answer:

    for (let i = 0; i < 3; i++) { for (let j = 0; j < 2; j++) { } } -> Option A
  4. Quick Check:

    JS nested loops use for(;;) inside for(;;) [OK]
Hint: Look for 'for (let i=0; ...)' syntax in JavaScript [OK]
Common Mistakes:
  • Using Python or other language syntax
  • Missing curly braces {}
  • Incorrect loop keywords
3. What is the output of this code?
for (let i = 1; i <= 2; i++) {
for (let j = 1; j <= 3; j++) {
console.log(i * j);
}
}
medium
A. 1 2 3 1 2 3
B. 6 5 4 3 2 1
C. 1 4 9 2 5 8
D. 1 2 3 2 4 6

Solution

  1. Step 1: Trace outer loop values

    Outer loop i runs 1 and 2.
  2. Step 2: Calculate inner loop outputs

    For i=1, j=1,2,3 outputs 1*1=1, 1*2=2, 1*3=3.
    For i=2, j=1,2,3 outputs 2*1=2, 2*2=4, 2*3=6.
  3. Final Answer:

    1 2 3 2 4 6 -> Option D
  4. Quick Check:

    Multiply i and j in nested loops [OK]
Hint: Multiply outer and inner loop counters [OK]
Common Mistakes:
  • Mixing up i and j values
  • Not running inner loop fully each time
  • Confusing output order
4. Find the error in this nested loop code:
for (let i = 0; i < 3; i++) {
for (let i = 0; i < 2; i++) {
console.log(i);
}
}
medium
A. Outer loop condition is wrong
B. Missing semicolon after for loops
C. Inner loop uses same variable name 'i', causing conflict
D. Console.log syntax is incorrect

Solution

  1. Step 1: Check variable names in loops

    Both loops use variable 'i', inner loop redeclares 'i' shadowing outer.
  2. Step 2: Explain why this is a problem

    Using same variable name causes confusion and unexpected behavior in nested loops.
  3. Final Answer:

    Inner loop uses same variable name 'i', causing conflict -> Option C
  4. Quick Check:

    Use different variables for nested loops [OK]
Hint: Use different variable names for each loop [OK]
Common Mistakes:
  • Reusing same variable name in nested loops
  • Ignoring variable scope issues
  • Assuming semicolons are mandatory after for()
5. You want to print a 3x3 grid of stars (*) using nested loops. Which code correctly does this?
hard
A. for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { console.log('*'); } }
B. for (let i = 0; i < 3; i++) { let row = ''; for (let j = 0; j < 3; j++) { row += '*'; } console.log(row); }
C. for (let i = 0; i < 3; i++) { console.log('***'); }
D. for (let i = 0; i < 3; i++) { let row = '*'; for (let j = 0; j < 3; j++) { row += '*'; } console.log(row); }

Solution

  1. Step 1: Understand the goal

    We want 3 rows, each with 3 stars printed together as a line.
  2. Step 2: Check each option's output

    for (let i = 0; i < 3; i++) { let row = ''; for (let j = 0; j < 3; j++) { row += '*'; } console.log(row); } builds a string of 3 stars per row and prints it 3 times.
    for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { console.log('*'); } } prints stars individually, not as rows.
    for (let i = 0; i < 3; i++) { console.log('***'); } prints 3 stars but only 3 times, no loop for columns.
    for (let i = 0; i < 3; i++) { let row = '*'; for (let j = 0; j < 3; j++) { row += '*'; } console.log(row); } starts with '*' and adds three more '*', printing four stars per row.
  3. Final Answer:

    for (let i = 0; i < 3; i++) { let row = ''; for (let j = 0; j < 3; j++) { row += '*'; } console.log(row); } -> Option B
  4. Quick Check:

    Build row string inside outer loop [OK]
Hint: Build each row string inside outer loop [OK]
Common Mistakes:
  • Printing stars one by one without grouping
  • Not resetting row string inside outer loop
  • Confusing loops for rows and columns
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