LearnFrenzy
Sign in Take a free test

Array Methods · Find the Error · Question 7

Error: passing a string instead of a function Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

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

main.js
const users = [{ name: "Asha", active: true }, { name: "Ravi", active: false }];
const active = users.filter("active");
console.log(active);
  • A It prints [ { name: 'Asha', active: true } ]
  • B It prints []
  • C It prints both users
  • D TypeError: "active" is not a function – filter needs a callback: users.filter((u) => u.active) Correct
Correct Answer: Option D

Array methods do not accept property names (unlike some libraries such as lodash). Pass a function that reads the property.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links