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 trialjohn larson
16,594 PointsWhat 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
Treehouse TeacherWell, 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!
john larson
16,594 Pointschrome, Thanks Jenifer...I'll just scrap my code and start over...again
Tom Emody
1,808 PointsI 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.
Joshua Elder
2,767 PointsHave you made sure you are saving the calculator.js file and then reloading the dice simulator page?
john larson
16,594 Pointsjohn larson
16,594 PointsI thought I called it with calculator.equals(), but I'm still getting undefined at that point.
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherI 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:
arajay
4,463 Pointsarajay
4,463 Pointscalculator.equals() returns 'undefined' in firefox, but returns the correct value in chrome.
changing the return function from
this.sum
tocaculator.sum;
resolves the issue.