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

Get one of the answers to be correct in a quiz. So the player gets one more point!

Making a quiz where the user have 3 answer options, I need to make one of the answers correct and the two other false for each object in the array. I'm thinking I need to get the indexNumber of the array and then set one of the key's to true for each object in the array. And then use an if/else function to compare the users answer to the correct answer. Am I on to something there? or Is there another better way?

I'm not sure how to tell the computer that this answers is correct and this answers is wrong. Can I do that in the array?

And since I have 3 options for the player to choose between, I'm thinking that I would connect the button/answer ID to the different keys in the object array, would that work?

Here is my code now

https://jsfiddle.net/82sz7j6q/4/

1 Answer

Steven Parker
Steven Parker
243,199 Points

Why not add a "correct" value?

In the questions array, for each question, in addition to the three answers you could have another property to indicate which answer is the correct one. You might call this property "correct" and it would contain either 1, 2, or 3.

Then, when an answer button is clicked, you just see if it matches the "correct" value for that question. If so, add the point and give notice of a correct choice, and if not just give notice of an incorrect choice.