Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Abhijit Das
Abhijit Das
5,022 Points

the finished calculator seems not working properly

var calculator = {
  sum: 0,
  add: function(value) {
    this.sum += value;
  },
  subtract: function(value) {
    this.sum -= value;
  },
  multiply: function(value) {
    this.sum *= value;
  },
  divide: function(value) {
    this.sum /= value;
  },
  clear: function() {
    this.sum = 0;
  }, 
  equals: function() {
    return this.sum;
  }
};

I passed the code challenge with above code and when I did try this in browser console it seems add function is working, however multiply is coming 0. don't know Why? I am getting result like below.

calculator.add(20);
undefined
calculator.add(5);
undefined
calculator.equals();
25
calculator.multiply(20);
undefined
calculator.multiply(5);
undefined
calculator.equals();
0

3 Answers

Steven Parker
Steven Parker
216,120 Points

I got a different result.

When I tried it, I got 2500 as the final result. Maybe you had a typo somewhere?

Also, for working with this in the console, you might like to put return in front of each function body line (except for "equals", which has one already). That way, as you call each method, you'll see the new sum instead of "undefined".

calculator.add(20);
20
calculator.add(5);
25
calculator.multiply(20);
500
calculator.multiply(5);
2500
Abhijit Das
Abhijit Das
5,022 Points

Hi Steven Parker i am still confused, when as i did type in console the series of calculation such as, calculator.add(10),calculator.add(5),calculator.multiply(20) and calculator.returns(); i am getting the result 300. However whenever I tried only multiplication such as, calculator.multiply(20) and calculator.multiply(15), and type calculator.equals() i am getting only 0 in my console. So it seems that i can't randomly do here the calculation?

Steven Parker
Steven Parker
216,120 Points

Your calculator is fine, the problem is just math. Since you start with 0, anything you multiply it with will still give you 0.

So always put the very first number in using add, no matter what you intend to do with the next one.

Abhijit Das
Abhijit Das
5,022 Points

Steven Parker Now it seems clear, I was wondering how could someone get the result by doing only the multiplication. When the sum value is 0; Now it's much more clear to me . Thank you.