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 trialAbhijit Das
5,022 Pointsthe 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
231,275 PointsI 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
5,022 PointsHi 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
231,275 PointsYour 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
5,022 PointsSteven 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.