简体   繁体   中英

How to create a grading scale in HTML using JavaSript?

I am just trying to get some help. I did my research but I couldn't find any helpful sources; This is my first time doing Java and I am not trying to get my homework solved; I am just trying to have a hint! so if there is anyone who can help me I would apprentice it!

This is my first time doing Java, and my teacher wants me to do grade scaling using JavaSript in HTML. However, he wants the result to appear in an alert. Like if the input is 90 then the page will alert "Grade is A" He gave me an example but I need to add code to it and I am not sure what to add...Please guys help me

<html>
<head>
    <script>
    function myFunction(){
         var x = document.getElementById("score").value;
         if (true) {
            alert("Hello World")
        }
}       
</script>
</head>

<body>  
    <p>Enter score in the box:</p>
    <input type="text" id="score">
    <button onclick="myFunction()">click</button>
</body>
</html> 

I tried to add this

 if (x >= 90) {
  grade = "A";
  if (x >= 80) {
  grade = "B";
} //and so on//

You'll want an else if statement if that's how you're going to lay this out. Though, I would recommend a switch in a case like this https://www.w3schools.com/js/js_switch.asp

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
} 

You'll probably want to confirm the value is numeric but this should be enough to get you started on the letter grade logic:

if (x >= 90){
    grade = "A";
}
else if (x >= 80) 
{
    grade = "B";
}
else if (x >= 70) 
{
    grade = "C";
}
else if (x >= 60) 
{
    grade = "D";
}
else{
    grade = "F";
}

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