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
|| gradeInput === 1.5regex?===won't work, the value returned by prompt is a string. ;-)<3and>7are also going to work unexpectedly, and OP is going to want to throw in agradeInput = Number(gradeInput)somewhere