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

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