LearnFrenzy
Sign in Take a free test

Loops · Exercise 3 of 4

Loops Find the Error

  • 15 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Debug fifteen JavaScript loop programs: SyntaxErrors from break in callbacks, commas in a for header and missing labels, TypeErrors from non-iterables and off-by-one reads, and silent bugs from for…in on arrays, splice in a forward loop and forEach used to search.

Question 11 Easy

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const bills = [250, 400, 150];
let total = 0;
for (let i = 0; i <= bills.length; i++) {
  total += bills[i];
}
console.log("Total:", total);
A total must be declared with const
B i <= bills.length reads bills[3], which is undefined, and 800 + undefined is NaN – use i < bills.length
C bills must be converted to numbers
D += cannot be used in a loop
Question page & discussion
Question 12 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const scores = [10, 20, 30];
let total = 0;
for (const i in scores) {
  total += i;
}
console.log("Total:", total);
A scores must be declared with let
B for…in skips the first element
C for…in gives the index keys as strings, so total becomes "0012" – use for (const s of scores) total += s
D total should start at ""
Question page & discussion
Question 13 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const nums = [2, 4, 5, 6, 8];
for (let i = 0; i < nums.length; i++) {
  if (nums[i] % 2 === 0) nums.splice(i, 1);
}
console.log(nums);
A splice does not work inside loops
B After splice, the next element moves into index i but the loop moves on to i + 1, so it is skipped – loop backwards or use filter
C % 2 does not detect even numbers
D nums must be declared with let
Question page & discussion
Question 14 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
function findUser(users, id) {
  users.forEach((u) => {
    if (u.id === id) return u;
  });
}
const users = [{ id: 1, name: "Asha" }, { id: 2, name: "Ravi" }];
console.log(findUser(users, 2));
A return u only returns from the forEach callback, and findUser itself returns nothing – use return users.find((u) => u.id === id)
B The ids must be compared with ==
C forEach cannot read objects
D users must be passed by reference
Question page & discussion
Question 15 Easy

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
let n = 5;
const out = [];
while (n < 0) {
  out.push(n);
  n--;
}
console.log("Countdown:", out, "n =", n);
A while loops need a do before them
B The condition n < 0 is false from the start, so the body never runs – it should be n > 0
C n-- should be n++
D out must be a string
Question page & discussion
Up next · Exercise 4Loops Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links