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

if statement within the for loop is not incrementing the "correctAnswers" variable - even though syntax looks correct

correctAnswers variable always comes back as 0 even when getting questions right and should be incrementing. The problem appears to be happening in the for loop if statement.

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) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

// Ask the questions

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

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

}

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

Can you try in for loop for the last statement i++

1 Answer

Since you convert your responses to lowercase and all your answers are in title case they will never be equal. One easy fix is to change your answers to lowercase.

Annalee Pawlowski
Annalee Pawlowski
1,144 Points

Thank you! For some reason I thought the toLowerCase was also converting the question string but now the error seems obvious. Thanks!