Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Object-Oriented JavaScript (2015) Practice Project Question Prototype

Simon Tanner
Simon Tanner
5,776 Points

question.js lines 7-9

'''javascript Question.prototype.isCorrectAnswer = function (choice) { return this.answer === choice; }; '''

Please could somebody tell me why you need to use 'return' in the above. Why not just 'this.answer === choice'?

'''javascript Quiz.prototype.guess = function(answer) { if(this.getCurrentQuestion().isCorrectAnswer(answer)) { this.score++; } '''

Then, if my thinking is right, the 'return this.answer' becomes the argument in 'function(answer)', but then I'm struggling to understand how chaining the methods works in the above code...

Simon Tanner
Simon Tanner
5,776 Points

sorry didn't do back ticks...I'll try again:

Question.prototype.isCorrectAnswer = function (choice) { return this.answer === choice; }; 

Please could somebody tell me why you need to use return in the above. Why not just this.answer === choice?

 Quiz.prototype.guess = function(answer) { if(this.getCurrentQuestion().isCorrectAnswer(answer)) { this.score++; }

Then, if my thinking is right, the return this.answer becomes the argument in function(answer), but then I'm struggling to understand how chaining the methods works in the above code...

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,705 Points

As i understand it, chaining is how you share the properties of one function with another function. So instead of having 2 functions with the same properties you share them out, define them in one function and make them available in the other.

Returning the value is a way of telling the function to run the code in the function than then break out of that function and move on.

Not the most technical explanation but i think it's right. :)

Simon Tanner
Simon Tanner
5,776 Points

Thanks Jonathan, that's really helpful!

Jess Hines
Jess Hines
5,411 Points

You're returning a value so that you can call the function on an object and use it in a conditional statement. If you don't have a return, your code will still run, but the result of the comparison will be immediately lost because it doesn't get out of the function call, it would be a local evaluation. Returning is what lets you get a value back out so that when you call isCorrectAnswer(choice) you get back true or false.