LearnFrenzy
Sign in Take a free test

Scope and Hoisting · Find the Error · Question 11

Bug: the price shows the wrong currency Answer, explanation & discussion

  • Find the error
  • Medium
  • 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 currency = "USD";
function format(amount) {
  return `${amount} ${currency}`;
}
function indianPrice(amount) {
  const currency = "INR";
  return format(amount);
}
console.log(indianPrice(500));
  • A Template literals cannot use outer variables
  • B Scope is lexical: format() reads the top-level currency, not the caller's local one – pass the currency as a parameter Correct
  • C const currency inside indianPrice is a SyntaxError
  • D format() should be an arrow function
Correct Answer: Option B

A function never sees its caller's local variables. function format(amount, currency = "USD") and format(amount, "INR") fixes it.

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