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
Zachary Luke
Front End Web Development Techdegree Graduate 17,646 PointsMade a JavaScript quiz. The final if/else statements don't work the way I hoped?
I just wrote a simple JS quiz game for fun. But the last few if/else statements don't work as intended.
It should display a message along with how many out of five the player gets right. But you can purposely fail, and it'll say you got no less than 2/5 right. Can someone help me understand why it doesn't work?
THE CODE IN QUESTION:
var correct = 0;
var answer1 = prompt("What is something guys wear at weddings?")
if (answer1.toUpperCase() === 'TUXEDO' || answer1.toUpperCase() === 'TUXEDOS'){
correct += 1;
}
var answer2 = prompt("How much do restaurant servers make?")
if (answer2.toUpperCase() === '2.13' || '$2.13' || '2.13 per hour' || '$2.13 per hour' || '2.13/hour' || '$2.13/hour' || '2.13/hr' || '$2.13/hr'){
correct += 1;
}
var answer3 = prompt("How much does does a Costco annual membership cost?")
if (answer3.toUpperCase() === '$60'){
correct += 1;
}
var answer4 = prompt("What is the 27th digit in Pi (3.14...)?")
if (answer4.toUpperCase() === '3'){
correct += 1;
}
var answer5 = prompt("What is Zach really good at?")
if (answer5.toUpperCase() === 'PHOTOGRAPHY'){
correct += 1;
}
if (correct === 5) {
document.write("<h1>You are a smart cookie! You got " + correct + "out of 5 right! Great job!!</h1>");
} else if (correct >= 3) {
document.write("<h1>Not bad, you got " + correct + " out of 5 right. Try to get them all!</h1>");
} else if (correct >= 1) {
document.write("<h1>Okay, you got " + correct + " out of 5 right. Try again!</h1>");
} else {
document.write("<h1>...Seriously? You answered " + correct + "of of 5 correctly. You need to study more, dude.</h1>");
}
1 Answer
Steven Parker
243,186 PointsWhen combining comparisons, be sure to use complete comparison expressions. For example:
if (answer1.toUpperCase() === 'TUXEDO' || 'TUXEDOS')
This test will always be true because a non-empty string is considered "truthy", and an "or" combination ("||") is true when either side is true.
But you can test for two different answers this way:
if (answer1.toUpperCase() === 'TUXEDO' || answer1.toUpperCase() === 'TUXEDOS')
If you have many answers to compare, you might make an array of them and use the "includes" method to test the answer:
var correctAnswers = ['2.13', '$2.13', '2.13 per hour', '$2.13 per hour',
'2.13/hour', '$2.13/hour', '2.13/hr', '$2.13/hr'];
if (correctAnswers.includes(answer2.toLowerCase()))
Steven Parker
243,186 PointsSteven Parker
243,186 PointsUse the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area.
Or watch this video on code formatting.