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 Modifying Objects with Methods

What am I missing, getting undefined...

//at this point we are running it in the console with...
//calculator.add(2) 
//calculator.add(5)
//calculator.equals()
//and I'm getting undefined

 var calculator = {
    sum: 0,
    add: function(value){
    this.sum += value; // update the value w/sum
  },
    clear: function(){
    this.sum = 0;
    },
    equals: function(){
    return this.sum
    }
}

4 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Well, you're missing one semicolon. But that's not affecting anything. Remember that while you're adding, nothing is being returned. The return doesn't happen until you call/invoke the equals method. This can also be seen as having undefined in the console at 2:53 in the video. It does this until Andrew calls the equals method when it then actually returns something.

Hope this helps! :sparkles:

I thought I called it with calculator.equals(), but I'm still getting undefined at that point.

//I copied this out of the console


calculator.add(2)
undefined
calculator.add(2)
undefined
calculator.equals()
undefined
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I have absolutely no idea! I'm running your code, in both Firefox and Chrome (even with the missing semicolon), and I'm getting back the result when I do calculator.equals(). What browser are you using?

This is from my console in Chrome:

 var calculator = {
    sum: 0,
    add: function(value){
    this.sum += value; // update the value w/sum
  },
    clear: function(){
    this.sum = 0;
    },
    equals: function(){
    return this.sum
    }
}
undefined
calculator.add(2)
undefined
calculator.add(3)
undefined
calculator.equals()
5

calculator.equals() returns 'undefined' in firefox, but returns the correct value in chrome.

changing the return function from this.sum to caculator.sum; resolves the issue.

chrome, Thanks Jenifer...I'll just scrap my code and start over...again

Tom Emody
Tom Emody
1,808 Points

I have this same problem. For some reason when I run this code as a preview through teamtreehouse, it gives me undefined when i do calculator.equals()

It also seems to not actually change the value of sum. However if I open up a console other than the preview one from treehouse, it works.

Have you made sure you are saving the calculator.js file and then reloading the dice simulator page?