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

Build a Quiz Challenge, Part 1 Solution

Hi guys, I don't know why, but the ( correctAnswer) does not increase?

var questions = [
    ["Where is arizona state located? " ,  "USA"],
    ["How many states are in the United States?  ", "50"],
    ["What's my name ", "Thayer"]
];
var correctAnswer = 0;
var wrongAnswer = 0;
var question;
var answer;
var response;
var result;

function print(message) {
    document.write(message);
}
for (var i = 0; i < questions.length; i +=1) {
    question = questions[i][0];
    answer = questions[i][1];
    response = parseInt(prompt(question));
    if (response === answer) {
        correctAnswer +=1;

    }else{
        wrongAnswer +=1;
    }
}
result = "You got  " + correctAnswer + " question(s) right " + " And " + wrongAnswer + " questtion(s) wrong! ";
print(result);

3 Answers

Steven Parker
Steven Parker
229,732 Points

Remove the parseInt.

You're applying parseInt to the user's input from prompt to convert the response into a number. But the correct answers that you compare with are all strings.

Hi Steven,

its the same code in the solution video. it also didn't work when i tried that before, like this way.

    response = prompt(question);
    response= parseInt(response);
Steven Parker
Steven Parker
229,732 Points

You don't need parseInt at all. You want to compare strings with strings.

    response = prompt(question);  // change only this line
Anthony T
Anthony T
7,060 Points

I noticed this, too. It seems to be wrong in the video. It works for the code in the video because the instructor's answers are all numbers.

Albert D
Albert D
998 Points

try this :

var question = []; var answer = []; var count = 0; var correctIndex = 0; var incorrectIndex = []; question.push('Which team won the champions league in 2017?','Which country hosts the 2018 Fifa World Cup?','which country has the biggest army in the world?','Which country does not have an army but has a football team?','Which programming language is used for machine learnking and creating classifiers?'); answer.push('real madrid','russia','usa','iceland','python'); function startQuiz(question,answer) { for(var i = 0;i<question.length;i++) { var getAnswer = prompt(question[i]); getAnswer = getAnswer.toLowerCase(); if(getAnswer===answer[i]) { count++; correctIndex = i; } else { incorrectIndex.push(i); } } displayFinalScore(count,incorrectIndex); }

function displayFinalScore(count,incorrectIndex) { document.writeln("Congratulations you have got "+count+" answers right have scorred "+count*10); document.writeln("<p>following the list of incorrect questions with their answers<p>"); for(var i = 0;i<incorrectIndex.length;i++) { document.write("<p>Question : "+question[incorrectIndex[i]]+"</p>"); document.write("<p>Answer : "+answer[incorrectIndex[i]]+"</p>"); } }

startQuiz(question,answer);

Thank you, Albert D

Steven Parker
Steven Parker
229,732 Points

When posting code, be sure to use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down: Or watch this video on code formatting.