Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Object-Oriented JavaScript (2015) Introduction to Methods Modifying Objects with Methods

Uncaught TypeError: calculator.add is not a function

What am I doing wrong?

var calculator = {
        sum: 0,
        add: function(value) {
      this.sum = this.sum + value;

    },
    clear: function() {
      this.sum = 0;

    }, 
    equals: function() {
      return this.sum;

    }
}

ignore the script tags. I only posted it there to preview the Syntax highlighting

Can you provide the code where you're calling Calculator.add()?

1 Answer

Steven Parker
Steven Parker
231,248 Points

:point_right: I don't see any problem.

I tried this code and got a "32" displayed in the console:

calculator.clear();
calculator.add(21);
calculator.add(11);
console.log(calculator.equals());

I think Mikis is on the right track — it must be something odd where you call it.

One other thing to consider, unlike function definitions, object assignments are not "hoisted". So if you tried to call this function before the definition, it would fail.