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 trialDalibor Nikolic
5,283 Pointsmy solution
Ok so i did the challenge but i didn't do it his way this might be best practice at all. Could someone explain to me why you can write >= 3 instead of doing it like i did in my soultion and how it doesn't override the correct answer of 5?
var correctAnswers = 0;
var question1 = prompt("What color does the apple have?");
var question2 = prompt("How many legs does a spider have?");
var question3 = prompt("How many keys does a piano have?");
var question4 = prompt("What is the name of a marvel character that has a brother named Loki?");
var question5 = prompt("What is the latest marvel movie?");
var answer1 = "red";
var answer2 = "6";
var answer3 = "88";
var answer4 = "thor";
var answer5 = "strange";
if(question1.toLowerCase() === answer1){
correctAnswers++;
}
if(question2.toLowerCase() === answer2){
correctAnswers++;
}
if(question3.toLowerCase() === answer3){
correctAnswers++;
}
if(question4.toLowerCase() === answer4){
correctAnswers++;
}
if(question5.toLowerCase() === answer5){
correctAnswers++;
}
document.write("<p>You got " + correctAnswers + " right answers!</p>");
if(correctAnswers === 5)
{
document.write("<p>You got a gold crown!</p>");
}
else if(correctAnswers === 4 || correctAnswers === 3)
{
document.write("<p>You got a silver crown!</p>");
}
else if(correctAnswers === 2 || correctAnswers === 1)
{
document.write("<p>You got a bronze crown</p>");
}
else
{
document.write("<p>Better luck next time!</p>");
}
Dalibor Nikolic
5,283 PointsI was thinking of ant but it became a spider in my head haha
1 Answer
Jason Anello
Courses Plus Student 94,610 PointsHi Dalibor,
I think you're asking about this code
if(correctAnswers === 5)
{
document.write("<p>You got a gold crown!</p>");
}
else if(correctAnswers === 4 || correctAnswers === 3)
{
document.write("<p>You got a silver crown!</p>");
}
vs.
if(correctAnswers === 5)
{
document.write("<p>You got a gold crown!</p>");
}
else if(correctAnswers >= 3)
{
document.write("<p>You got a silver crown!</p>");
}
Whenever you have if, else if, else
only one of those will ever be executed. Either the first condition that is true will be executed or the else
block will be executed if none of the conditions are true.
So if correctAnswers is 5 the first condition is true and it will output "gold crown" and the rest of the conditions will be skipped. There's no danger that it will reach the second condition of correctAnswers >= 3
and accidentally outputting "silver crown".
It does make that condition true but it never reaches it.
The advantage to doing it as in the video is that it's less typing and less chance for making an error. Imagine if there were more questions and maybe 5 to 10 correct answers for silver crown, You would have a pretty long conditional if you had to check all the numbers individually from 5 to 10.
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsA little note: Spiders all have 8 legs, not 6
LOL