LearnFrenzy
Sign in Take a free test

Array Methods · Exercise 3 of 4

Array Methods Find the Error

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

Debug fifteen programs that use array methods: SyntaxErrors in callbacks, TypeErrors from empty reduces, missing arrays, non-function callbacks, find results and Sets, and silent bugs from map(parseInt), forEach results, reduce without an initial value, truthy filter results and string comparators.

Question 11 Medium

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

main.js
const qtyInputs = ["5", "12", "8"];          // values from form fields
const qtys = qtyInputs.map(parseInt);
console.log(qtys);
A parseInt cannot read form values
B The array must be converted with Array.from
C map passes the index as parseInt's radix – use qtyInputs.map(Number) or (s) => parseInt(s, 10)
D map skips the first element
Question page & discussion
Question 12 Easy

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

main.js
const prices = [100, 250, 400];
const discounted = prices.forEach((p) => p * 0.9);
console.log(discounted);
A forEach always returns undefined – use map to collect the new values
B The callback needs braces
C 0.9 must be written as 90 / 100
D prices must be declared with let
Question page & discussion
Question 13 Hard

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

main.js
const orders = [{ amount: 100 }, { amount: 200 }, { amount: 300 }];
const total = orders.reduce((sum, o) => sum + o.amount);
console.log(total);
A amount must be a string
B reduce cannot read objects
C The callback parameters are in the wrong order
D Without an initial value the first accumulator is the first order object, so + turns it into a string – pass 0 as the initial value
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
const users = [{ name: "Ravi", admin: false }, { name: "Meera", admin: false }];
if (users.filter((u) => u.admin)) {
  console.log("Show admin panel");
} else {
  console.log("No admins");
}
A filter returns false when nothing matches
B admin must be a string
C The else branch needs braces
D filter returns an array, and even an empty array is truthy – use users.some((u) => u.admin)
Question page & discussion
Question 15 Medium

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

main.js
const names = ["Meera", "Asha", "Ravi"];
names.sort((a, b) => a - b);
console.log(names);
A Subtracting strings gives NaN, which sort treats like 0 ("equal"), so the order never changes – use a.localeCompare(b)
B sort cannot sort strings
C The comparator must return booleans
D names must be a const
Question page & discussion
Up next · Exercise 4Array Methods Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links