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 trialMUZ140759 Kelvin Mutukura
6,549 PointsImplement the subtract method
I'm stuck here
var calculator = {
sum: 0,
add: function(value) {
this.sum += value;
},
subtract: function(value) {
},
multiply: function(value) {
},
divide: function(value) {
},
clear: function() {
this.sum = 0;
},
equals: function() {
return this.sum;
}
}
3 Answers
Colton Ehrman
Courses Plus Student 5,859 PointsNot really sure how your calculator should work, but I would think that subtract would be the opposite of add so
this.sum -= value;
MUZ140759 Kelvin Mutukura
6,549 PointsThanks Colton
Colton Ehrman
Courses Plus Student 5,859 PointsNo problem :)
Colton Ehrman
Courses Plus Student 5,859 PointsCheck out this way of doing your calculator
var calculator = function(val){
this.sum = val;
this.add = function(value) {
this.sum += value;
return this;
};
this.subtract = function(value) {
};
this.multiply = function(value) {
};
this.divide = function(value) {
};
this.clear = function() {
this.sum = 0;
};
this.equals = function() {
return this.sum;
};
return this;
};
console.log(calculator(6).add(6).equals());
Now just implement the other methods as I did with this.add, and now you can chain methods and then when your ready to see the end result just call equals.
jeannieduen
Front End Web Development Techdegree Student 8,709 PointsWhat is the answer to this challenge!?!?!? I am forever stuck on this.
Shaun Wong
14,489 PointsDid you see the solution above? Just in case you are still stuck forever... i've given you the answer below.
Check out this for shorthand multiply and division https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators
var calculator = {
sum: 0,
add: function(value) {
this.sum += value;
},
subtract: function(value) {
this.sum -= value;
},
multiply: function(value) {
},
divide: function(value) {
},
clear: function() {
this.sum = 0;
},
equals: function() {
return this.sum;
}
}
Christian Andersson
8,712 PointsChristian Andersson
8,712 PointsSorry for posting this off-topic, but I'm helping Treehouse investigate a potential issue with their webserver. Please follow this link and leave a comment saying if you have experienced this webserver issue or not.
https://teamtreehouse.com/forum/attention-treehouse-webserver-issues-please-read
Thank you.