0

I need to validate a user input for a GPA calculator based on the following criteria:

  • Is a number
  • Is an integer value between 3 and 7, or 1.5 floating point value.

So far I have tried the following:

var gradeInputMessage = "Please enter your  grade for course " +userInput;
var gradeInput = "";

gradeInput = prompt(gradeInputMessage);

while (
  isNaN(gradeInput) || //grade input is a number
      gradeInput <3 || gradeInput >7
)
{
alert ("invalid Course Grade. Please try again");
gradeInput = prompt(gradeInputMessage);
}

This seems to work fine for the numbers 3,4,5,6,7, but I have no idea how to include 1.5

Please excuse any ignorance on my behalf, this is a university course that I have to complete, and I have never done any form of programming, nor do I do any for work.

Appreciate any help/feedback.

Cheers, Mitchell

5
  • just add || gradeInput === 1.5 Commented Apr 18, 2017 at 1:17
  • 1
    Maybe it's time to use some regex? Commented Apr 18, 2017 at 1:18
  • 1
    you could use an if statement separately just before the while to check "if(gradeInput == 1.5)". do you intend to return some data or do you have some code below the while loop? Commented Apr 18, 2017 at 1:20
  • 3
    @Hamms— === won't work, the value returned by prompt is a string. ;-) Commented Apr 18, 2017 at 1:34
  • ah, true! In that case, <3 and >7 are also going to work unexpectedly, and OP is going to want to throw in a gradeInput = Number(gradeInput) somewhere Commented Apr 18, 2017 at 1:38

2 Answers 2

2

Probably easier to just check for the things that you want, then not them.

Also added a null checker so people can hit cancel to exit out of the prompt loop.

And I convert the string into a number by putting + in front of gradeInput

var gradeInputMessage = "Please enter your  grade for course ";

var gradeInput = prompt(gradeInputMessage);

while (
  gradeInput !== null &&
  (
  isNaN(gradeInput) || //grade input is a number
      !((+gradeInput >=3 && +gradeInput <=7) || +gradeInput === 1.5)
  )
)
{
alert ("invalid Course Grade. Please try again");
gradeInput = prompt(gradeInputMessage);
}

Sign up to request clarification or add additional context in comments.

2 Comments

as @RobG pointed out, you'll want to make sure to convert the result of prompt to a number before check it
@Hamms ah, yes, I added Number to the bottom, but forgot the top, I could also just do with soft checking :\
0

Are you doing a web app?

html 5 with type="number" input can valid the number for you.

I'm using jQuery here but it can be done in pure js easily. also validGpa.indexOf(gpa) this is pure js (.indexOf())

my example here use an Array to save all valid GPA, if any input match then print msg xx is valid. If not matching, say 'Sorry ...'

var validGpa = ['3', '4', '5', '6', '7', '1.5'];
$('#inputgpa').on('change', function() {
  var gpa = $(this).val();
  if (validGpa.indexOf(gpa) > -1) {
    $('#validation').text(gpa + ' is Valid!');
  } else {
    $('#validation').text('Sorry! ->' + gpa + '<- is not a valid GPA...');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<span>Enter your GPA now:<span><br>
<input id="inputgpa" type="number" /><br>
<span id="validation"></span>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.