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

Why does my .toLowerCase() function NOT work inside the for loop?

The .toLowerCase() function in the following code is not working? Why is this happening?

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

let qa = [ ["What is the speed of light (in km/s)?","300000"], ["What is the third closest planet to the Sun?","earth"], ["When did the Second World War end","1945"]
];

let html_1 = "; let html_2 = "; let corr_ans = 0; let answer;

for (let i = 0; i<qa.length; i++){

answer = prompt (qa[i][0]);

answer.toLowerCase();

console.log(answer); // I have put this line of code to see if .toLowerCase() function works

if (answer === qa[i][1]){
html_1 += "<li>"+qa[i][0]+"</li>";
corr_ans++; }else{ html_2 += "<li>"+qa[i][0]+"</li>"; }

}

print("<h2>You answered "+corr_ans+" question(s) correctly!");

print("<ol>Questions with correct answer: "+html_1+"</ol>")

print("<ol>Questions with incorrect answer: "+html_2+"</ol>");

2 Answers

Note: The toLowerCase() method does not change the original string.

Basically, toLowerCase() makes a lowercase COPY of the string.

If you don't overwrite the original string with the copy, or use the copy somewhere, then it doesn't really do anything.

So instead of

answer.toLowerCase()

you can do

answer = answer.toLowerCase()

// alternatively
const lowerCaseAnswer = answer.toLowerCase();

Thank you, all!