LearnFrenzy
Sign in Take a free test

Loops · Exercise 3 of 4

Loops Find the Error

  • 15 questions
  • Page 1 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 1 Medium

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
const orders = [120, 450, 9000, 300];
orders.forEach((amount) => {
  if (amount > 5000) break;
  console.log("Processing", amount);
});
A It processes 120 and 450, then stops
B It processes all four orders
C TypeError: break is not a function
D SyntaxError: break is illegal inside the callback function – use for…of with break
Question page & discussion
Question 2 Easy

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
for (let i = 0, i < 5, i++) {
  console.log(i);
}
A It prints 0 to 4
B SyntaxError: the three parts of a for header are separated by semicolons – for (let i = 0; i < 5; i++)
C It loops forever
D ReferenceError: i is not defined
Question page & discussion
Question 3 Medium

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
for (let r = 0; r < 3; r++) {
  for (let c = 0; c < 3; c++) {
    if (r === c && r > 0) break outer;
  }
}
console.log("done");
A SyntaxError: there is no label called outer – add outer: before the first for
B It prints done
C It breaks only the inner loop
D ReferenceError: outer is not defined
Question page & discussion
Question 4 Medium

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
function check(value) {
  if (value < 0) continue;
  return Math.sqrt(value);
}
console.log(check(16));
A It prints 4
B It prints NaN
C SyntaxError: continue can only be used inside a loop – return early instead
D It returns undefined for negatives
Question page & discussion
Question 5 Easy

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
let attempts = 0;
do {
  attempts++;
  console.log("Attempt", attempts);
}
console.log("finished");
A It prints Attempt 1 and finished
B It loops forever
C SyntaxError: a do block must be followed by while (condition)
D It prints finished only
Question page & discussion
Question 6 Easy

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const marks = { maths: 90, science: 85 };
for (const m of marks) {
  console.log(m);
}
A TypeError: marks is not iterable – use Object.values(marks) or Object.entries(marks)
B It prints maths and science
C It prints 90 and 85
D SyntaxError
Question page & discussion
Question 7 Medium

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
function printTags(post) {
  for (const tag of post.tags) console.log("#" + tag);
}
printTags({ title: "Hello", tags: null });
A TypeError: post.tags is null and null is not iterable – use post.tags ?? []
B Nothing is printed
C It prints #null
D SyntaxError
Question page & discussion
Question 8 Easy

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const stock = { pens: 40, books: 12 };
stock.forEach((qty) => console.log(qty));
A TypeError: stock.forEach is not a function – objects have no forEach; use Object.values(stock).forEach
B It prints 40 and 12
C It prints pens and books
D Nothing is printed
Question page & discussion
Question 9 Medium

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const users = [{ name: "Asha" }, { name: "Ravi" }];
for (let i = 0; i <= users.length; i++) {
  console.log(users[i].name.toUpperCase());
}
A It prints ASHA and RAVI only
B SyntaxError before anything runs
C It prints ASHA and RAVI, then TypeError: users[2] is undefined because i <= users.length runs one step too far
D It prints undefined at the end
Question page & discussion
Question 10 Medium

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const count = 3;
for (const n of count) {
  console.log("Row", n);
}
A It prints Row 0 to Row 2
B It prints Row 1 to Row 3
C TypeError: count is not iterable – numbers cannot be iterated; use a classic for loop
D It prints nothing
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links