LearnFrenzy
Sign in Take a free test

Closures · Find the Error · Question 7

Error: calling the object a factory returned Answer, explanation & discussion

  • Find the error
  • Easy
  • 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
function makeCounter() {
  let n = 0;
  return { inc: () => ++n, get: () => n };
}
const counter = makeCounter();
counter();
  • A TypeError: counter is not a function – the factory returns an object; call counter.inc() Correct
  • B It increments n
  • C It returns 0
  • D ReferenceError
Correct Answer: Option A

Check what the factory returns: here an object of methods, not a single function.

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