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 trial

JavaScript

Thomas Santoro
Thomas Santoro
4,007 Points

getCurrentQuestion function returning as 'undefined', breaks buttons

https://w.trhou.se/n359xqrwzd I get the following error and can't find my mistake:

Uncaught TypeError: Cannot read property 'isCorrectAnswer' of undefined at Quiz.guess (quiz.js:12)

Here is the function called by the button click handler:

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

BUT it seems like the problem goes back to the getCurrentQuestion function (see snapshot linked above), which looks OK to me! What am I missing?

i dont see you metion isCorrectAnswer any where in your code beacuse i rember doing this few days ago and i i am pretty sure i had to metion isCorrectAnswer like define it but i would check the next vid for the soultion i know ur really close.

2 Answers

Steven Parker
Steven Parker
229,981 Points

The question is not advancing.

I only get the error you describe if I click an answer button 4 times or more. Since the question is not advancing, after the third click you run out of question objects so getCurrentQuestion starts returning undefined.

I think you're probably just missing this line at the end of the Quiz.prototype.guess function:

    QuizUI.displayNext();

Thanks for providing the workspace snapshot to facilitate analysis of the issue.

Thomas Santoro
Thomas Santoro
4,007 Points

@Steven P. - Yep, that did it! Thanks!
So my take away is that the bug can really be anywhere in the code...I need to really understand how everything meshes together because the console tools will only get you so far.

[Context for others reading this from the community, I had 3 questions, not two; hence the 4 clicks that it took to get the error. I had also noticed it took a few clicks to get the error but had missed that this was a significant clue].