Reputation: 9
I am trying to creat a little website that ask user a number from a prompt. then a message will appear on a div tag that tell users what grade they got. it doesn't work.
Exemple: if I got 90%, a message will appear and tell me that my grade is A+.
here are the codes:
<h1>Prelab1 Ex2</h1><br>
<div id="div2"></div>
<script>
var inputperc = prompt("Enter a percentage");
if(inputperc > 100 || inputperc < 0 || isNaN(inputperc)){
document.getElementById("div2").innerHTML = "Enter a valid Percentage value between 0 and 100";
else{
if(inputperc >= 90 && inputperc <= 100){
document.getElementById("div2").innerHTML = "A+";
}
if(inputperc>90 && inputperc<100){
document.getElementById("div2").innerHTML = "A+";
}
if(inputperc>85 && inputperc<89){
document.getElementById("div2").innerHTML = "A";
}
if(inputperc>80 && inputperc<84){
document.getElementById("div2").innerHTML = "A-";
}
if(inputperc>77 && inputperc<79){
document.getElementById("div2").innerHTML = "B+";
}
if(inputperc>73 && inputperc<76){
document.getElementById("div2").innerHTML = "B";
}
if(inputperc>70 && inputperc<72){
document.getElementById("div2").innerHTML = "B-";
}
if(inputperc>67 && inputperc<69){
document.getElementById("div2").innerHTML = "C+";
}
if(inputperc>63 && inputperc<66){
document.getElementById("div2").innerHTML = "C";
}
if(inputperc>50 && inputperc<62){
document.getElementById("div2").innerHTML = "C-";
}
if(inputperc>57 && inputperc<59){
document.getElementById("div2").innerHTML = "D+";
}
if(inputperc>53 && inputperc<56){
document.getElementById("div2").innerHTML = "D";
}
if(inputperc>50 v&& inputperc<52){
document.getElementById("div2").innerHTML = "D-";
}
if(inputperc>0 && inputperc<49){
document.getElementById("div2").innerHTML = "f";
}
}
}
</script>
Upvotes: -1
Views: 79
Reputation: 18557
Your code has a syntax error as spencer pointed out. You should consider writing a getGrade
function though.
var getGrade = function (percent) {
if (percent > 100 || percent < 0 || isNaN(percent)) {
return "Enter a valid Percentage value between 0 and 100";
}
if (percent >= 90 && percent <= 100) return "A+";
if (percent > 90 && percent < 100) return "A+";
if (percent > 85 && percent < 89) return "A";
if (percent > 80 && percent < 84) return "A-";
if (percent > 77 && percent < 79) return "B+";
if (percent > 73 && percent < 76) return "B";
if (percent > 70 && percent < 72) return "B-";
if (percent > 67 && percent < 69) return "C+";
if (percent > 63 && percent < 66) return "C";
if (percent > 50 && percent < 62) return "C-";
if (percent > 57 && percent < 59) return "D+";
if (percent > 53 && percent < 56) return "D";
if (percent > 50 && percent < 52) return "D-";
if (percent > 0 && percent < 49) return "F";
return "Unknown Error"
};
var el = document.getElementById("div2"),
percent = prompt("Enter a percentage:"),
grade = getGrade(percent);
el.innerHTML = grade;
Upvotes: 0
Reputation: 21565
Small error:
...
if(inputperc > 100 || inputperc < 0 || isNaN(inputperc)){
document.getElementById("div2").innerHTML = "Enter a valid Percentage value between 0 and 100";
} else { // <-- forgot to close the if.
if(inputperc >= 90 && inputperc <= 100){
...
Also as already mentioned it's a good idea to convert inputperc
to an Number with parseInt()
. Also make sure to remove the extra }
at the end because of this.
Upvotes: 0
Reputation: 8793
Whenever a user enters text into an input field it is considered a string
. So you need to convert it to an integer
. parseInt
around your prompt is the easiest way. the 10
is just saying that you're using 0,1,2,3,4,5,6,7,8,9 (base10)... not something like 0100100101 (base2).
var inputperc = parseInt(prompt("Enter a percentage"), 10);
Upvotes: 1