Click here to Skip to main content
13,143,637 members (31,365 online)
Rate this:
 
Please Sign up or sign in to vote.
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:
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 12-Dec-16 2:28am
Updated 12-Dec-16 3:28am
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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?");
// 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.
  Permalink  
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.
Peter Leow 12-Dec-16 9:01am
   
I have just finished typing the complete solution. Check it out.
Peter Leow 12-Dec-16 9:03am
   
As I said, there is no need a loop.
Denisowator 12-Dec-16 9:14am
   
There is. I need the prompt at the top, to display again, every single time after the error displays, and the input to be checked again until valid input has been given.
Peter Leow 12-Dec-16 9:22am
   
I was typing and your comment came in. So my guess is correct. See my reply below.
Peter Leow 12-Dec-16 9:15am
   
Could it be that you intend to re-prompt the user for valid input, then you will need to do the validation and prompt in a do...while loop prior to the if-else statements for grading the score. Figure it out yourself first. If you encounter difficulty after trying, post it as a new question.
Jochen Arndt 12-Dec-16 9:16am
   
Shouldn't it be

if (isNaN(gameScore) || gameScore < 0 || gameScore > 100)

Anyway, good answer
Peter Leow 12-Dec-16 9:18am
   
Yes, you are right. Corrected. Thank you.
Jochen Arndt 12-Dec-16 9:22am
   
Are you going for a new record in edited answers?

The '!' has to be removed too.

Peter Leow 12-Dec-16 9:25am
   
Thanks again. It's removed. I have been busy typing away.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Because of
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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 12 Dec 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100