Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have a class assessment, and I have to write code that will display a message based on a score given by the user, and will display an error message if they input doesn't fit the parameters.

Here is the whole code:
JavaScript
gameScore = 0;
gameScore = prompt("What was your game score?");

while (gameScore >= 0 && gameScore <= 100) {

    if (!isNaN(gameScore) && gameScore >= 91 && gameScore <= 100) {
        alert("Good job");
        break;
    }

    else if (!isNaN(gameScore) && gameScore >= 81 && gameScore <= 90) {
        alert("Pretty good");
        break;
    }

    else if (!isNaN(gameScore) && gameScore >= 61 && gameScore <= 80) {
        alert("Passed");
        break;
    }

    else if (!isNaN(gameScore) && gameScore >= 40 && gameScore <= 60) {
        alert("Not so good");
        break;
    }

    else if (!isNaN(gameScore) && gameScore >= 0 && gameScore < 40) {
        alert("Failed");
        break;
    }

    else {
    alert("Please provide a valid score (0-100).");
    }
}


Every statement works fine, except for the "else" one. I have no idea what I'm doing wrong. When I run the code, the text appears to input a score, and if I input something outside 0-100, or something other than a number, it doesn't show the alert in the else block.

Thanks for any and all help and suggestions.

What I have tried:

I tried adding "isNaN(gameScore) && " to the while statement at the beginning, but the else block still does't execute.
Posted
Updated 12-Dec-16 3:28am
v3

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:
C#
gameScore = 0;
gameScore = prompt("What was your game score?");
// validation should come first to weed out all bad inputs
// change && to || and remove !, thanks to Jochen Arndt twice, my oversight twice.
if (isNaN(gameScore) || gameScore < 0 || gameScore > 100) {
	alert("Please provide a valid score (0-100).");
} else if (gameScore >= 91) { // it is understood from 91 to 100
	alert("Good job");
} else if (gameScore >= 81) { // it is understood from 81 to <91
	alert("Pretty good");
} else { // asumming anything below 81 failed
	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.
 
Share this answer
 
v10
Comments
CPallini 12-Dec-16 8:42am    
5.
Peter Leow 12-Dec-16 9:02am    
Thank you. I was still typing and noticed it has already been accepted.
Denisowator 12-Dec-16 8:45am    
I'm putting "break;" so that the loop stops after one of the messages was displayed.
Peter Leow 12-Dec-16 9:03am    
Why do you need a loop in the first place?
Denisowator 12-Dec-16 8:52am    
How can I include other values in the while statement? I tried using isNaN() to make it display if the person inputs only text, but that didn't work.
Because of
C++
while (gameScore >= 0 && gameScore <= 100) {

which ensure that gameScore is valid.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900