How to display error message when using a grade calculator in JavaScript? -
below grade calculator attach letter grade number entered, using if/else statements. having trouble finding way display error message if out of ranger number or non numeric value entered. suggestions? thanks!
var entry; var lettergrade; entry = prompt("enter number grade 0 through 100\n" + "or enter 999 end entries", 999); entry = parseint(entry); if (entry <= 59) lettergrade = "f"; else if (entry >= 60 && entry <= 69) lettergrade = "d"; else if (entry >= 70 && entry <= 79) lettergrade = "c"; else if (entry >= 80 && entry <= 89) lettergrade = "b"; else if (entry >= 90 && entry <= 100) lettergrade = "a"; alert("number grade = " + entry + "\n" + "letter grade = " + lettergrade);
well out of range >100 can cover else in end:
[..] else if (entry >= 90 && entry <= 100) lettergrade = "a"; else alert("error, number " + entry + " out of range (>100)");
as not being number, can use isnan():
if(isnan(entry)){ alert("that not number!"); else { if (entry <= 59) lettergrade = "f"; .... }
edit: see input >0<101 catch negative numbers need add first if statement:
if (entry >= 0 && entry <= 59)
Comments
Post a Comment