LearnFrenzy
Sign in Take a free test

Objects · Find the Error · Question 11

Bug: every lookup returns undefined Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

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

main.js
const prices = { pen: 10, book: 50, bag: 899 };
function priceOf(item) {
  return prices.item;
}
console.log(priceOf("book"), priceOf("bag"));
  • A prices.item reads a property literally named "item" – use prices[item] Correct
  • B The keys must be quoted
  • C item must be converted to a number
  • D Functions cannot read outer objects
Correct Answer: Option A

Dot notation never evaluates the name. prices[item] uses the parameter's value and prints 50 899.

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