Debug fifteen JavaScript object programs: SyntaxErrors in literals, methods and accessors; runtime errors from null objects, lost this, locked properties and invalid or circular JSON; and silent bugs from dot notation with variables, shallow copies, truthiness checks, inherited keys and nested defaults.
Question 11Easy
The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?
The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?
main.js
const base = { currency: "INR" };
const prices = Object.create(base);
prices.pen = 10;
prices.book = 50;
let total = 0;
for (const k in prices) total += prices[k];
console.log(total);
AObject.create copies base into prices
Bprices.pen must be a string
C+= cannot add numbers
Dfor…in also visits the inherited currency key, adding "INR" – loop over Object.values(prices) instead
Correct Answer: Option D
for…in walks enumerable keys of the prototype chain too. Object.values / Object.entries list own properties only, giving 60.
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·