JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Annalee Pawlowski
Annalee Pawlowski
1,144 Points

response = prompt(question); is not storing users input. Instead it stores the but question string from the array.

I have followed this tutorial code exactly and keep running into the same error. The response = prompt(question); is storing the original question string from the quizQuestions array rather than what the users typed in answer is, therefore :

if (response === answer)

will always return false.

The Also had this problem when trying to code prior to watching the answer video... What am I doing wrong?

// My Code

var quizQuestions = [ [ 'What is the color of the sun?' , 'Yellow' ] , [ 'What is the color of the grass?' , 'Green' ] , [ 'What is the color of the sky?' , 'Blue'] , ]

var correctAnswers = 0; var question; var answer; var response; var html;

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

// Ask the questions

for (var i = 0; i < quizQuestions.length; i += 1) { question = quizQuestions[i][0]; answer = quizQuestions[i][1]; response = prompt(question); response = question.toLowerCase();

if (response === answer) { correctAnswers += 1; }

}

html = "You got " + correctAnswers + " question(s) correct."; print(html);

// You can see it is incorrect by console.log response or by getting the answers right and noting that they are not added to the correctAnswer count.

1 Answer

Steven Parker
Steven Parker
180,205 Points

The prompt isn't the issue, but the line after it is replacing the result with the question:

    response = prompt(question);        // this gets the user's answer into "response" 
    response = question.toLowerCase();  // this replaces "response" with question (in lower case)

Perhaps you intended to convert the reponse to lower case. You can do that on the same line:

    response = prompt(question).toLowerCase();

Also, to format posted code as shown here, 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.