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

Michael Watson
Michael Watson
13,204 Points

Object multiply function not working

Can't find info online for this scenario, and can't seem to get it to work.

calculator.js
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) {

  },
  clear: function() {
    this.sum = 0;
  }, 
  equals: function() {
    return this.sum;
  }
}

2 Answers

Try

this.sum *= value;

Michael Watson
Michael Watson
13,204 Points

Thanks for the response. It appears that since there is no += equivalent for multiplication or division, I had to use this.sum = this.sum * value;

Taken directly from Microsoft's Website. You'll notice that x *= y and x /= y are both listed.

https://msdn.microsoft.com/en-us/library/6a71f45d.aspx

Assignment and Lambda Operators


These operators have higher precedence than the next section and lower precedence than the previous section. NOTE, you can click on the operators to go the detailed pages with examples.

x = y – assignment.

x += y – increment. Add the value of y to the value of x, store the result in x, and return the new value. If x designates an event, then y must be an appropriate function that C# adds as an event handler.

x -= y – decrement. Subtract the value of y from the value of x, store the result in x, and return the new value. If x designates an event, then y must be an appropriate function that C# removes as an event handler

x *= y – multiplication assignment. Multiply the value of y to the value of x, store the result in x, and return the new value.

x /= y – division assignment. Divide the value of x by the value of y, store the result in x, and return the new value.

x %= y – modulus assignment. Divide the value of x by the value of y, store the remainder in x, and return the new value.

x &= y – AND assignment. AND the value of y with the value of x, store the result in x, and return the new value.

x |= y – OR assignment. OR the value of y with the value of x, store the result in x, and return the new value.

x ^= y – XOR assignment. XOR the value of y with the value of x, store the result in x, and return the new value.

x <<= y – left-shift assignment. Shift the value of x left by y places, store the result in x, and return the new value.

x >>= y – right-shift assignment. Shift the value of x right by y places, store the result in x, and return the new value.

=> – lambda declaration