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 Finishing the Calculator

Implement the subtract method

I'm stuck here

calculator.js
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;
  }
}

Sorry 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.

3 Answers

Not really sure how your calculator should work, but I would think that subtract would be the opposite of add so

this.sum -= value;

Thanks Colton

No problem :)

Check 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.

What is the answer to this challenge!?!?!? I am forever stuck on this.

Shaun Wong
Shaun Wong
14,489 Points

Did 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;
  }
}