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

Gabriel Ward
Gabriel Ward
20,222 Points

Building the quiz

I'm trying to build the quiz bit by bit. So far I've got a working for loop that goes through three questions. I'm stuck on keeping track of which are correct and which are incorrect. Here's my code so far:

var questions = [
    ["what is the number of states in USA?", "50"],
    ["what is the capital of NZ?", "Wellington"],
    ["What is 1 + 1", "2"]
]

var correct = 0
var incorrect = 0;

for (var i = 0; i < questions.length; i++) {
    var answer = prompt(questions[i][0]);
}
if(answer === questions) {
    correct += 1;

} else {
    incorrect +=1;

}

console.log(correct);
console.log(incorrect);


}

In the console, it says that

correct is 0 and incorrect is 1, no matter what i put for the answers

1 Answer

Luciano Bruzzoni
Luciano Bruzzoni
15,518 Points

Hello, try putting your if-else statement inside the for loop, the way you have it right now, your only checking for the correct answer once, after the whole for loop finishes.

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

   if(answer === questions) {
       correct += 1;
    } else {
        incorrect +=1;
    }
} //end for loop

Good luck :)