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

Michael Curtis
Michael Curtis
6,684 Points

Why is my if statement always evaluating true?

/*Questions
1. What is my name - Mike
2. How old am I - 29
3. Where was I Born - Cardiff
*/

var questions = [
                ["What is my name?","Mike"],
                ["How old am I?","29"],
                ["Where Was I born?","Cardiff"]
                ];
var score=0;

//arrays of integers representing the index in questions of the correct / incorrect questions
var incorrect=[];
var correct=[];

var i=0;
do {

var answer=prompt(questions[i][0]);
var questionAnswer=questions[i][1];
  if (answer.toLowerCase === questionAnswer.toLowerCase) {

    //correct
    console.log("correct "+questions[i][1]+"-"+answer);
    score+=1;
    correct.push(i);

  }else {

    incorrect.push(i);

  }

i+=1;  
}while(i<questions.length)

why is the if statement always evaluating as true? the console log outs are showing that the variables are being assigned to what I expect, but even when they are clearly different, they do not seem to be evaluating as false.

any help appreciated

1 Answer

toLowerCase is a function. It needs parentheses.

your if statement should be:

if (answer.toLowerCase() === questionAnswer.toLowerCase()) {