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 trialsasha shveps
842 Pointswhy this doesnt work?
// 1. Create a multidimensional array to hold quiz questions and answers const questionAnswer = [ ['What is your name?', 'Max'], ['What is your hobby?', 'Coding'], ['What is your shoe size?', '45'],
];
const question1 = prompt( questionAnswer[0][0]) const question2 = prompt( questionAnswer[1][0]) const question3 = prompt( questionAnswer[2][0]) let correctAnswers = 0; // 2. Store the number of questions answered correctly
for (let i = 0; i> questionAnswer.length; i++){ if (question1 === questionAnswer[0][1] ) { correctAnswers++; } else if (question2 === questionAnswer[1][1] ) { correctAnswers++; } else if (+question3 === questionAnswer[2][1]) { correctAnswers++; } } /*
- Use a loop to cycle through each question
- Present each question to the user
- Compare the user's response to answer in the array
- If the response matches the answer, the number of correctly answered questions increments by 1 */
// 4. Display the number of correct answers to the user
document.querySelector('main').innerHTML= You answered ${correctAnswers} correct questions.
1 Answer
Steven Parker
231,269 PointsOne issue I spotted offhand:
In this comparison +question3 === questionAnswer[2][1]
The "+" symbol at the start converts question3 into a number, but the answer stored in questionAnswer[2][1] is a string. The type-sensitive equality operator (===
) will not match a number to a string even if the value is correct. So you either need a standard comparison (==
), or to not convert the answer to a number. or to store the correct answer as a number instead of a string.
For future questions, use Markdown formatting to preserve the code's appearance. You may also want to take Markdown formatting to preserve the code's appearance, and perhaps this one about sharing a snapshot of your workspace.