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 trialjlampstack
23,932 PointsQuiz.prototype.guess question about parameter
I keep getting crossed up when reading the guess function. Can anyone clarify my understanding? The guess function has 1 parameter, which is argument, which is passed into the isCorrectAnswer(choice) function as an argument when we call it from the question.js file.
Where I keep getting crossed up is, why are we using answer
in the guess function, and choice
in the isCorrectAnswer function? Is there a reason for this? Wouldn't it make more sense to use choice
for both? Or even answer
for both? Since a choice is an answer, and an answer is a choice? I tried it this way and it works.
I think I'm getting crossed up because there are so many pieces to this equation, it can be mind-boggling. Any feedback is appreciated. Thanks in advance!
quiz.js file
Quiz.prototype.guess = function(choice) {
if(this.getCurrentQuestion().isCorrectAnswer(choice)) {
this.score++;
}
this.currentQuestionIndex++;
};
question.js file
Question.prototype.isCorrectAnswer = function (choice) {
return this.answer === choice;
};
1 Answer
Steven Parker
231,269 PointsThe main thing to keep in mind here is that when you define a function, the parameter name is only a placeholder for the real value that will be used when the program runs. The parameter name has no bearing on program operation, it could be "joe" or "noodle" or "uq42" and it would make no difference. It only needs to be used consistently within the function.
But for clarity, a name is usually chosen based on what the parameter will be used for in the function. While "answer" would have been appropriate in both cases, "choice" was probably used in the latter function to eliminate confusion when it is compared to the "answer" property of the Question object.
jlampstack
23,932 Pointsjlampstack
23,932 PointsThanks again Steven! I'm understanding but guess it takes some getting use to until I'm more comfortable with it.