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 trialThomas Marren
3,626 PointsAlways saying the answer is correct no matter what answer I give
Hello,
Whenever I run my program Question 2 will always be marked correct no matter what answer I give. The only way I've found to fix it is to remove the "50" as a possible correct answer. I would still like 50 and fifty to be possible answers. What am I doing wrong?
Here is my code
var userName = prompt("What is your name?");
var userScore = 0;
alert("Hello " + userName + ". I'm going to ask you five questions. Are you ready?");
//Question 1
var quest1 = prompt("What is the capital of New York?");
if (quest1.toLowerCase() === "albany"){
userScore += 1;
}
console.log(userScore);
//Question 2
var quest2 = prompt("How many states are in the US?");
if (quest2.toLowerCase() === "50" || "fifty") {
userScore += 1;
}
console.log(userScore);
//Question 3
var quest3 = prompt("What is the best drink in the world?");
if (quest3.toLowerCase() === "beer"){
userScore += 1;
}
console.log(userScore);
//Tell them their score
if (userScore === 0){
alert("You got " + userScore + " out of 3 correct");
}
else if (userScore === 1){
alert("You got " + userScore + " out of 3 correct");
}
else if (userScore === 2){
alert("You got " + userScore + " out of 3 correct");
}
else {
alert("You got " + userScore + " out of 3 correct");
}
1 Answer
Erik McClintock
45,783 PointsThomas,
Update your if conditional for question 2 to the following:
if( quest2.toLowerCase() === '50' || quest2.toLowerCase() === 'fifty' )
And you should be good to go!
Erik
Thomas Marren
3,626 PointsThomas Marren
3,626 PointsThanks Erik! Is that always the case when using variables in conditional statements? You always have to repeat the variable for each possible answer/condition?
Erik McClintock
45,783 PointsErik McClintock
45,783 PointsThomas,
If you are using an if statement, then yes, unfortunately, you do need to write out the variable to check each time. For just two checks in this case ("50" and "fifty"), it's not so bad, but you do have other options that may work a little more neatly should your conditions scale. You could look into switch statements, or using an array with the indexOf method, or even regular expressions!
Erik