Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Jennifer Hughes
Jennifer Hughes
11,421 Points

toLowerCase() not working.

Hi!

I cannot figure out why my toLowerCase() method is not working.

Can anyone help?

Thanks!

Here is my code:

var answer;
var correct;
var incorrect;
var html;
var questions = [

  ["What is the name of the president of the USA?", "barack obama"],
  ["Who won the super bowl yesterday?", "broncos"],
  ["What color is the sun?", "yellow"]

];


//console.log(questions[2]);

  for(var i=0; i<questions.length; i++) {
    answer = prompt(questions[i][0]);
    answer.toLowerCase();
    if(answer === questions[i][1]) {
      correct++;
    }
    else {
      incorrect++;
    }
  }
  html = "Congratulations, you got " + correct + " answers correct!";



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

 print(html);

2 Answers

Hi Jennifer,

Do you need to assign the result of the toLowerCase() method? (I'm not a JavaScript programmer so don't know).

The method may not have an impact on the object so answer stays the same but if you assign to another variable, that may be the lowercase version of answer:

guesswork.js
var answer = "ANSWER";
var anotherAnswer = answer.toLowerCase();
// answer still holds "ANSWER"

// or assign back to itself 
answer = answer.toLowerCase();

Just guessing - might be worth a try. :wink: :+1:

Steve.

Per Karlsson
Per Karlsson
12,683 Points

Hi Jennifer,

Like Steve said, you need to save the conversion in another variable.

Or you could just use it directly in the if clause like this:

 if(answer.toLowerCase() == questions[i][1])

Also initiate your var correct to 0.

~Per