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

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
221,450 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].