1. That is because of this line of code:
while (gameScore >= 0 && gameScore <= 100)
Quote:
something outside 0-100, or something other than a number
will never enter this while loop.
2. Why are you putting
break;
inside the if statements?
3. Why do you need to repeatedly checking this after the first if statement?
!isNaN(gameScore)
Take a look at this example, and use it as a guide to improve your code:
gameScore = 0;
gameScore = prompt("What was your game score?");
if (isNaN(gameScore) || gameScore < 0 || gameScore > 100) {
alert("Please provide a valid score (0-100).");
} else if (gameScore >= 91) {
alert("Good job");
} else if (gameScore >= 81) {
alert("Pretty good");
} else {
alert("failed!");
}
As shown in the above example, I always tell my students these:
1. Validation check always come first so as to weed out all illegal inputs before other valid operations set in.
2. The if-else statements are inter-dependent, with proper planning, you can eliminate redundant conditions that have already been covered by prior if-else statements.