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 trialSAMUEL LAWRENCE
Courses Plus Student 8,447 PointsFinish the calculator
Hi guys, in the final challenge of the Object oriented javaScript
lesson, we were asked to finish a calculator app. We had to do the subtract, multiply and divide
part. I tried out the app in my text editor and I kept getting all the wrong answers. like 50 * 5 would equal 0 or 5 * 25 would equal 2500. However, in the code challenge it was saying the answers are correct. Here's the app
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;
}
}
Video link: https://teamtreehouse.com/library/objectoriented-javascript/introduction-to-methods/finishing-the-calculator
What's going on? Is the code correct?
1 Answer
Kevin Becerra
14,243 PointsThe app is working fine but since the way it was structured has you start at 0 and always sum equal a certain number it may be confusing to use. If you wanted to multiply 50 by 5 you had to make sure you first cleared the calculator and then added the 50 or 5, then you call the multiply function. Even there you would then have to remember you cleared it and so on. Whats adds on to it not being the best is that each time you called the functions you always get an undefined object which really doesn't allow you to know what the current sum is in between all the math you are trying to do.
Hope this helps!