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

Lucas Huerta-Murillo Grau
PLUS
Lucas Huerta-Murillo Grau
Courses Plus Student 1,365 Points

This is my code, I can't find the bug.

What ends up happening is that all the questions are pushed to the correct_answer array as if they were answered correctly even if they were not. Can anyone see where the bug is?

/* 
These are the arrays needed to store the user answers as 
correct and incorrect, and there is the array to store 
the questions and correct answers.
*/

var user_answer;
var correct_answer = [ ];
var incorrect_answer = [ ];
var questions_answers = [
  ["How many continents are there?", "7"],
  ["What is the capital of France?", "Paris"],
  ["Who is the president of the USA?", "Donald Trump"]
];

// This function prints an array as a numbered list

function printList( list ) {
  var listHTML = '<ol>';
  for (var i = 0; i < list.length; i += 1) {
    listHTML += '<li>' + list[i] + '</li>';
  }
  listHTML += '</ol>';
  print(listHTML);
}

// Replaces document.write

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

/*This is the loop that asks and checks the questions, 
this is what isn't working
*/

for (var i = 0; i < 3; i +=1) {
  user_answer = prompt(questions_answers[i][0]);
  if ( user_answer.toLowerCase === questions_answers[i][1].toLowerCase ) {
   correct_answer.push( questions_answers[i][0] );
  } else {
   incorrect_answer.push( questions_answers[i][0] );
  }
}

// What will show after the user answers

print("<h2>You got these questions right!</h2>");
printList( correct_answer );
print("<h2>You got these questions wrong.</h2>");
printList( incorrect_answer );

1 Answer

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

one thing i notice is that you are not properly calling toLowerCase. it is a method and must be followed by parens (), so myString.toLowerCase().