Challenge - 5 Problems
Nested Loops Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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());
Attempts:
2 left
💡 Hint
Remember that the inner loop breaks when j equals 2, so it only adds the first pair for each i.
✗ Incorrect
The inner loop stops when j is 2, so only j=1 is added for each i from 1 to 3, resulting in "1-1 2-1 3-1".
❓ Predict Output
intermediate2: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);
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.
✗ Incorrect
The inner loop runs 0 + 1 + 2 + 3 = 6 times in total.
❓ Predict Output
advanced2: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());
Attempts:
2 left
💡 Hint
The continue with label skips the rest of the outer loop iteration when i equals j.
✗ Incorrect
When i equals j, continue outer skips the remaining iterations of the inner loop and proceeds to the next i. i=1 skipped after first j=1, i=2 adds only '21 ', i=3 adds '31 32 '. Output: "21 31 32".
❓ Predict Output
advanced2: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(','));
Attempts:
2 left
💡 Hint
Add arr[i] + arr[j] starting with j = i, so pairs include sums of same and later elements.
✗ Incorrect
The sums are: 1+1=2, 1+2=3, 1+3=4, 2+2=4, 2+3=5, 3+3=6, so output is "2,3,4,4,5,6".
🧠 Conceptual
expert2:00remaining
Time complexity of nested loops
Consider this code:
What is the time complexity in Big O notation?
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?
Attempts:
2 left
💡 Hint
Sum the inner loop counts: 1 + 2 + 3 + ... + n.
✗ Incorrect
The total operations are 1 + 2 + ... + n = n(n+1)/2, which is O(n^2).