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 trialXun Huang
Web Development Techdegree Graduate 18,515 PointsIt's always 0 when I use "||" instead of ">=" .
here's my code:
/*
- Rank player based on number of correct answers
- 5 correct = Gold
- 3-4 correct = Silver
- 1-2 correct = Bronze
- 0 correct = No crown */
if (correct === 1 || correct === 2){
rank = "Bronze"
}else if(correct === 3 || correct === 4){
rank = "Silver"
}else if (correct === 5){
rank = "Gold"
}else {
rank = "None :("
}
Could someone tell me why?
1 Answer
Tim Danner
3,284 PointsYour code runs fine on my end. Have you counted the correct answers?
However, the codes could also be refactored with a switch
statement when there's too many if
statements involved:
let rank;
// play with this variable to see different results
let correct = 5;
switch (correct) {
case 5:
rank = "Gold";
break;
case 4:
case 3:
rank = "Silver";
break;
case 2:
case 1:
rank = "Bronze";
break;
default:
rank = "No crown";
break;
}
Xun Huang
Web Development Techdegree Graduate 18,515 PointsXun Huang
Web Development Techdegree Graduate 18,515 PointsThank you! And sorry I just found out why, I missed a parenthesis in other lines, this part is all good.