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

Quiz attempt- answers are always wrong?

Here is my code:

//ask questions -- use a two-dimensional array to store questions and answers
//display # of correct answers at end of quiz ---keep track of # of correct answers 
//use a conditional statement to scompare the response from the player to the answer in the array
//cycle through the questions using a for loop.

var questions = [
["What villian was formerly Captian America's sidekick Bucky?", "winter soldier"],
["In the DC universe, Which hero resembles Robin Hood?", "green arrow"],
["Which superhero beat superman in a foot race?", "flash"]
]

var correct= 0;
var incorrect= 0;

for (var i = 0; i < questions.length; i+= 1) {
  var answer= prompt(questions [i][0]);  
  if(answer.toLowerCase === questions [i][1]) {
    correct += 1;
  } else {
    incorrect += 1;
  }
}  

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

print("You got " + correct + " out of " + questions.length + " right!");
print("You got " + incorrect + " out of " + questions.length + " wrong.");

Every time I try it it, it tells my that I got 0 right. I know they are right but can't explain why it wont show it.

2 Answers

answer.toLowerCase() is a method, not a property, so it needs parenthesis. By not using parenthesis, you're comparing the function toLowerCase() itself to your answer, when what you want is to compare the result of the function to your answer. A function and a string can never be equal with a strict comparison (===) because their types are different.

You are right. I can not believe I overlooked such a simple thing. Thank you so much.

No problem. It is pretty easy to overlook - I always find myself mistakenly adding parenthesis to array.length, when it isn't actually a function!