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 does the var numCorrect not work above the for loop?

// https://teamtreehouse.com/community/cheatsheet doesn't seem to be working... //

var quiz = [
['Street number?', 21],
['(100 * 2) in Dollars?', 200],
['Avg. Pomodoro sessions per day?', 8]
];

var correct ='<ol>' + '<h2>Correct: </h2>';
var incorrect = '</ol><ol>' + '<h2>Wrong: </h2>';
var count = 0; 


for (var i = 0; i < quiz.length; i += 1){

    var answer = parseInt(prompt(quiz[i][0]));
    var count = count;
    if (quiz[i][1] === answer) {
        correct += '<li>' + quiz[i][0] + '</li>';
        count += 1;     
    }
    else {
        incorrect += '<li>' + quiz[i][0] + '</li>';
    }

}
// why do I need to place the var numCorrect after the for loop? //
var numCorrect = '<p>You have ' + count + ' correct questions! Anyways, keep on learning!</p>';
document.write(numCorrect);
document.write(correct);
document.write(incorrect);

1 Answer

Steven Parker
Steven Parker
231,269 Points

The count needed for numCorrect is done inside the for loop.

numCorrect does not work above the for loop because the count has not been made yet. But after the for loop is finished, the count is ready to show.

Thanks