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 trialSimon Tanner
5,776 Pointsquestion.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...
2 Answers
Jonathan Grieve
Treehouse Moderator 91,253 PointsAs 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
5,776 PointsThanks Jonathan, that's really helpful!
Jess Hines
5,411 PointsYou'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.
Simon Tanner
5,776 PointsSimon Tanner
5,776 Pointssorry 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 justthis.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 infunction(answer)
, but then I'm struggling to understand how chaining the methods works in the above code...