0
0
Javascriptprogramming~20 mins

Loop execution flow in Javascript - Practice Problems & Coding Challenges

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