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

Can someone please tell me why the correctAnswers count is not working in this code??

var quizList = [
    ['What is a programming language that is also a snake?', 'Python'],
    ['Who is the president of France?', 'Emmanuel Macron'],
    ['Which progeamming language is also a gem?', 'Ruby'],
    ['Which progamming language I\'m I learning right now?', 'JavaScript']
];
var correctAnswers = 0;
var ask;

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

function answerQuiz ( questions ) {
    listHTML = '<ol>'
        for (var i = 0; i < questions.length; i++) {
            ask = prompt(questions[i][0]);
            ask = ask.toLowerCase();
            if (ask === questions[i][1]) {
                correctAnswers ++;
            }
                listHTML += '<li>' + questions[i][0] + ' <strong>' + questions[i][1] + '</strong>'
        }
    listHTML += '</ol>'
    print(listHTML);
    print('You answered  ' + correctAnswers + '  questions correctly!');
}

answerQuiz(quizList);

Please and thank you!

https://w.trhou.se/orcb42htbd

3 Answers

Anton Klyuchkovksiy
Anton Klyuchkovksiy
8,327 Points

Oh, I get it :) You compare user input in lower case with answers, which are not.

ask = prompt(questions[i][0]);
            ask = ask.toLowerCase();
            if (ask === questions[i][1].toLowerCase()) {
                correctAnswers++;
            }

This one is works.

Or you can edit your replies in "questions" array:

"python", "emmanuel macron", "ruby", "javascript"

No, I just changed it on workspace and still not keeping count.

Oh I see! thanks! It works great! I didn't realize I had turn both of them to to lower case for it to match up.

Thanks Anton!