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 JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Why am I not getting 3 as my answer and only 1?

My solution is essentially the same as that in the Solution video, however, my counter is only telling me I'm getting 1 correct, not 3. Can someone tell me why?

var questions = [ ["Where are you from? ", " Ohio"], [" Who is your wife? ", "Ngoc"], [" How many kids do you have ", 2]]; var correctAnswers = 0; var question; var answer; var response;

function print(message) { document.write(message); }

for (var i = 0; i < questions.length; i++) { question = questions[i][0]; answer = questions[i][1]; response = parseInt(prompt(question));

if (response === answer) {
    correctAnswers += 1;    
}

}

print(correctAnswers);

2 Answers

Steven Parker
Steven Parker
229,785 Points

Only one of your questions has a number answer, the other two are strings. But your comparison code uses "parseInt" to convert the answer into a number, so it's not possible for the first two answers to match.

Also be aware that for the first answer you have " Ohio", so even a string comparison would fail unless you included the space in the response.

Ahh, awesome. Yeah, I saw that today and realized that this approach would only work for a number input.

Steven Parker
Steven Parker
229,785 Points

Erik Young — Glad to help. You can mark a question as solved by choosing a "best answer".

And happy coding!