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

Heidi Vasterling-Ford
Heidi Vasterling-Ford
7,805 Points

Trying to use .toLowerCase instead of parseInt, because my answers aren't numbers and it's not working. Anyone, help?

var correctAnswers = 0; var question; var answer; var response; var html; var questions = [[ 'What period of art is Picasso most commonly associated with?', 'cubism' ], ['What is the setting of Monets most famous painting?', 'garden'], ['What is the oldest sculpture of a woman we have today?', 'venus of willendorf'] ];

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

for ( var i = 0; i < questions.length; i += 1) { question = questions[i][0]; answer = questions[i][1]; response = answer.toLowercase(prompt(question)); if (response === answer) { correctAnswers += 1; } }

html = "You got " + correctAnswers + " correct!"; print(html);

//I believe my issue lies in the "response = " line of code, but I just can't crack it

1 Answer

Dave StSomeWhere
Dave StSomeWhere
19,808 Points

Your are correct, your line below doesn't make sense. First the 'c' in case needs to be upper case toLowerCase() and second, you are setting the response = to the answer and then checking to see if they are equal, your if statement will always return true.

response = answer.toLowercase(prompt(question));

// Try changing to this:
response = prompt(question).toLowerCase(); 
// or even better, to ensure answer is lower also - do the lower stuff in the if
response = prompt(question);

  if (response.toLowerCase() === answer.toLowerCase())