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 trialJeriah Bowers
Front End Web Development Techdegree Graduate 20,590 PointsJavaScript not running properly.
https://w.trhou.se/jdcw0naumy Ok, what's going on that I don't see? As you can se I'm trying to start a quiz program and i tested it after just one question with the alert(); And it runs fine when you answer correctly but it also gives you the one point even if your answer is wrong.. Why??
2 Answers
andren
28,558 PointsWhen you use the ||
operator you are not adding values to the first condition, you are adding separate independent conditions, meaning that all of the conditions you supply need to be meaningful enough that they could be used on their own. "five" is not by itself a very meaningful condition as it doesn't actually compare the string to anything, but it will evaluate to true
due to the way JavaScript converts non-empty strings to booleans.
In addition to that you have another issue in your code, you have forgotten to add parentheses after the toLowerCase
method. When you don't use parenthesis after a method in JavaScript you won't actually call the method, but instead just reference it, which is not what you want in your solution.
If you add the parenthesis and make the second condition one that is meaningful on it's own like this:
var score = 0;
var answer1 = prompt("How many Super Bowls have the Dallas Cowboys won?");
if (answer1.toLowerCase() === "5" || answer1.toLowerCase() === "five") {
score += 1
}
alert(score);
Then your code will work.
Thomas Nilsen
14,957 PointsBecause in your if, you have;
if (answer1.toLowerCase === "5" || "five")
"five" will always be evaluated as true, thats why you always get a point. Also your missing () in toLowerCase.
Try this;
if (answer1.toLowerCase() === "5" || answer1.toLowerCase() === "five")