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

tako izu
tako izu
13,453 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
216,012 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
13,453 Points

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