简体   繁体   中英

JavaScript how to ensure user enters a number between 1 and 100

Hello i want the user to enter a number between 1 and 100 any higher i want it to display array out of bounds and not add the number to the array here is what i have and it works but when u enter in another number it says NAN

for(var i = 0; Repeat !== 'n'; i++){
    Student.push(prompt("Enter Student Name: "));
    Mark.push(parseInt (prompt("Enter Student mark: ")));
    //Check if Mark entered is not out of bounds and gives a grade
    if (Mark[i] <0 || Mark[i] >100){
        alert("Grate out of bounds");
         Mark.splice(i);//Removes array if out of bounds
    }else if (Mark[i] >= 83){
        getGrade.push("A");
        A++;
    }else if (Mark[i] >= 70){
        getGrade.push ("B");
        B++;
    }else if (Mark[i] >= 50){
        getGrade.push ("C");
        C++;    
    }else if (Mark[i] >= 0){
        getGrade.push ("F");
        F++;
    //If mark is not a number
    }else if (isNaN(Mark)){
    alert("Must input numbers");
    }
    Repeat = prompt ("Do you want to enter another student: y/n");
}  

When you enter an incorrect value, the array is no longer in sync with i . For example, if you enter ( Bob and 1000 ) for the first grade, consider what gets stored into the variables after the call to Mark.splice(i) :

Student = [Bob]
Mark = []

Now Mark is empty, and i has been incremented to 1 . Let's say you now enter values Johnny and 50 . Your variables look like this:

Student = [Bob, Johnny]
Mark = [50]

Notice that when your code is validating the grade, there is no Mark[1] , because the array only contains one value and i is looking at the second entry - thus NaN . Also, you have more entries in Student than you do in Mark .


As a general principle, you should not "commit" the data until you have finished all your validation logic. So store the entered amounts in temporary variables, check them, and then finally add them to the array at the end once you have good input data:

var entryStudent = prompt("Enter Student Name: ");
var entryMark = parseInt (prompt("Enter Student mark: "));
//Check if Mark entered is not out of bounds and gives a grade
...
//Once you have determined the values are valid, add them to the array
if (valid) {
  Student.push(entryStudent);
  Mark.push(entryMark);
} else {
  // make sure i is moved back to previous entry so you don't skip one
  i--;
}

This code isn't complete (for example, you need to figure out how to determine whether valid is true or false ), but it should give you the general idea.

I would suggest 2 things -

  1. Make your "isNaN" check before any other checks (so you wouldn't insert wrong values).

  2. Store the parseInt result in a variable, make all the checks on that variable, and only if it passes - push it into Marks . That way you can avoid holding the i reference.

Something like this:

var grade, name;
for (var i = 0; Repeat !== 'n'; i++) {
    name = prompt("Enter Student Name: ");
    grade = parseInt(prompt("Enter Student mark: "));

    // check if grade entered is a number
    if (isNaN(grade)) {
        alert("Must input numbers");
        continue;
    }
    // check if grade entered is not out of bounds
    if (grade < 0 || grade > 100) {
        alert("Grade out of bounds");
        continue;        
    }
    // store the data
    Student.push(name);
    Mark.push(grade);

    if (grade >= 83) {
        getGrade.push("A");
        A++;
    } else if (grade >= 70) {
        getGrade.push("B");
        B++;
    } else if (grade >= 50) {
        getGrade.push("C");
        C++;
    } else if (grade >= 0) {
        getGrade.push("F");
        F++;
    }
    Repeat = prompt("Do you want to enter another student: y/n");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM