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

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);

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!