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

tako izu
tako izu
17,394 Points

Not showing how many I got right

I'm making a quiz from JavaScript and when it reaches the end it shows you how many you got right. But it's not showing how many I got. This is the code I did:

var questions = ["What's the capital of France?", "What's 10 * 1000?", "If I have 5 apples in one hand, and 6 apples in the other, how many apples do I have?"]; var answers = ["paris", 10000, "11 apples"]; var answerToThis;

function quiz() { var correct = 0; for (var i = 0; i < questions.length; i += 1) { answerToThis = prompt(questions[i]); if (answerToThis.toLowerCase === answers[i]) { correct += 1; } } document.getElementById("final").innerHTML = "You got " + correct + " question(s) correct."; }

Then it comes up with 'You got 0 questions correct.' even though I got all of them.

1 Answer

Steven Parker
Steven Parker
230,274 Points

When calling a method, you must put parentheses after the method name even if it takes no arguments (like "toLowerCase()").

Also, be aware that when using the type-sensitive comparison ("==="), a string (like you get back from "prompt") will not match a number even if they represent the same value.

tako izu
tako izu
17,394 Points

Thank you for reminding me of that! I forget a lot sometimes... 😂