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

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!