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 Object-Oriented JavaScript (2015) Practice Project User Interface Code

Dmitri Antonov
PLUS
Dmitri Antonov
Courses Plus Student 6,010 Points

pls help with the code...

snapshot as follows: https://w.trhou.se/1e1bibx5x7

Hi guys,

Need help with the code. When onclick performed at quiz_ui.js nothing happens. Buttons remain same and next question is not showing... What am i doing wrong?...Can you suggest possible solutions. Looking forward and many thx....

6 Answers

Steven Parker
Steven Parker
231,269 Points

You seem to be missing a bit of program logic.

I notice in your click handlers, when the answer is correct you increment the score and question index, but do nothing else. And if the answer is not correct, the handler does nothing at all.

I would expect that the score would be incremented for a correct answer, but for either answer the index would be incremented and then the screen would get updated (perhaps a call to QuizUI.displayNext?)

Also the endQuiz function should probably not add to the index if it has already been incremented.

I'm sure there's a few more things to work on, but this should get you going again.

Dmitri Antonov
Dmitri Antonov
Courses Plus Student 6,010 Points

Hi Steven, thx for assistance. I've made some corrections. No i can make choices and score is appearing in the end, but it's all the time 2 of 2. It doesn't matter if i choose correct answers or not. What can be the problem?

Here is the snapshot: https://w.trhou.se/xh0budzff2

Dmitri Antonov
PLUS
Dmitri Antonov
Courses Plus Student 6,010 Points

Hi again, found a mistake. Was using "=" instead of correct "===" in if statement of assignButton click event handler)

Dmitri Antonov
PLUS
Dmitri Antonov
Courses Plus Student 6,010 Points

Steven, hi again:)

Is there any possibility to make assignChoice method in quiz_ui.js more complex (using for in loop for example). I mean if there are four buttons. It would be annoying to write everything separately. Looking forward.

thx

Steven Parker
Steven Parker
231,269 Points

You would need more changes than just assignChoice to add more buttons. The Question object, for example. But for an example of using a loop for this, see the displayChoices method in the video.

Dmitri Antonov
PLUS
Dmitri Antonov
Courses Plus Student 6,010 Points

One more question.... in teacher's video i don't see that he is assigning any values to the button. So by default it's just an empty button. But how then the choice is being handled to the button?

Steven Parker
Steven Parker
231,269 Points

The video does the same thing you are doing, by populating the innerHTML of the button with the answer.

Dmitri Antonov
PLUS
Dmitri Antonov
Courses Plus Student 6,010 Points

Hi Steven,

as far as i understand with my lack of javascript knowledge :)), only the innerHTML of paragraphs with id's "choice0" and "choice1" are being populated with quiz.getCurrentQuestion().choices[i]. The guessHandler method is only handling the button clicks and then the quiz.guess() inside it is checking if user's choice is correctAnswer?... But i don't see any functions that pass any values to buttons...Can you pls explain more detailed. Thx a lot....

P.S I've made several manipulations with console in development tools to check the value of the buttons and prevented new Question to appear by deleting the QuizUI.displayNext() function call after click event is processed just in case. Tried to check innerHTML and value of the buttons both before click event and after. So after typing in console for example document.getElementById("guess0").innerHTML, "select answer" is appearing in both cases( the innerHTML set while creating index.Html); and after typing document.getElementById("guess0").value, i receive just empty string "" in both cases. So the button is empty?...So how does the code work?)))...

I'm really confused abt it and trying to understand the code's behavior for the 3rd day.:D...Looking forward..

Steven Parker
Steven Parker
231,269 Points

We've gone a bit beyond the original question. Please mark it complete by choosing a "best answer".

There are no "values assigned to buttons". The only "value" the buttons get is the assignment of the innerHTML. When the button is clicked, this is compared to the correct answer to determine the score.

You don't need the console to see what the current value of innerHTML is. It can clearly be seen on the button itself!

Dmitri Antonov
PLUS
Dmitri Antonov
Courses Plus Student 6,010 Points

Sorry, but i still don't understand)))....I deleted the displayNext() in guessHandler(), so that no next question to be displayed, performed click on button, then checking the innerHTML of it and it's still "Select Answer" as by default. Nothing assigned to it., Where is this innerHTML which computer is comparing with answer? May be you can recommend some materials to study in order to understand what's going on?

Steven Parker
Steven Parker
231,269 Points

I'm not sure what you are using to look at the innerHTML value. Perhaps you are just seeing what it started as in the HTML file.

What is the label on the button itself? That will be the real innerHTML.