0
0
Javascriptprogramming~20 mins

Nested loops in Javascript - Practice Problems & Coding Challenges

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