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 trialroberthopman
16,502 PointsWhat does JavaScript do when it sees this if(this.getCurrentQuestion().isCorrectAnswer(answer))?
I'm reviewing the code of Andrew but I don't get this prototype on another prototype part. So I want to know how JS translates it. I understand that the output will be true/false. What are the smaller steps that JS takes?
3 Answers
Erik Nuber
20,629 Pointsanswer is being passed into the function that is being called. It is a string. It is being checked against the current question which is why 'this' is used. Read left to right, 'this' is what is being checked, it calls the method getCurrentQuestion() so we know which question we are dealing with and, it then calls the method isCorrectAnswer() to check to see if the parameter supplied, 'answer', is the correct answer to the given question.
Question.prototype.isCorrectAnswer(guess) {
return this.answer === guess
};
This prototype is a method that formulates the last part of what is being checked. We send in answer and, get a true or false of whether the answer we retrieved is the same as the correct answer.
Quiz.prototype.getCurrentQuestion = function() {
return this.questions[this.currentQuestionIndex];
};
This prototype gets the question we are currently on based on the current index.
So again, we are using 'this' to refer to where we are actually at and what we are specifically dealing with. We then call the method created by the prototype to get the current question. And finally, when we have the question, we need to check the answer so we call the method created again by another prototype and send in the users answer as a parameter. This is what is actually returning the true or false.
If the answer is correct (true) the score is incremented and the index is increased. If false, we are simply increasing the index.
roberthopman
16,502 PointsThanks
Ran ShemTov
14,148 Pointsjs executes the function and see what returns.
roberthopman
16,502 PointsDavid Curran try to take it one piece at the time. Please ask one of the teachers if you don't get it.
David Curran
7,682 PointsDavid Curran
7,682 PointsIm not quite understanding your explanation completely from Erik. When the getCurrentQuestion() method is called wouldn't the returned value just be an object which contains one of the two questions depending on the index. Therefore the getCurrentQuestion() would return say, {"Who was the first President of the United States?", [ "George Washington", "Thomas Jefferson" ], "George Washington"} I understand why you would run isCorrectAnswer(answer) but not sure how that would work when written like so: this.getCurrentQuestion().isCorrectAnswer(answer) Any information that would make this issue more understandable would be gratefully appreciated as I am slowly losing my mind with frustration.