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 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
210,837 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
210,837 Points

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

And happy coding!