简体   繁体   中英

How to display grade as an alert in javascript [on hold]

I have been trying to get this code to work, my assignment is to create a javascript where the user inputs a letter grade then it gives them an alert of what kind of grade they are gonna get. For some odd reason, I cant get the alert working. My teachers example involves using a function but I dont know what to put in there.

<html>
<head>
<script>
    Function(){
    var x = document.getElementById("score").value;
    if x >=1: {
    alert("invalid grade")
    }
    if .99>= x >=.9: {
    alert("A");
    }
    if .89>=x>=.8:{
    alert("B");
    }
    if .79>=x >=.7:{
    alert("C");
    }
    if .69>=x >=.6:Z{
    alert("D");
    }
    if x<=.59:{
    alert("F");
    }
{
</script>
</head>
<body>
    <p>Enter Score in the box:</p>
    <input type="text" id="score">
    <button onclick=print("x")">click</button
<body>

</html>

See all the comments below:

 <html> <head> </head> <body> <p>Enter Score in the box:</p> <input type="text" id="score"> <button>click</button> <!-- Your script should be just before the closing body tag so that by the time the browser reaches it, all the HTML will have been read into memory. Your teacher isn't really showing you the most correct technique if he/she told you to put it in the HEAD section. --> <script> // Set up event handling in JavaScript, not in HTML. Your teacher is wrong // to teach you that way. First, find the right HTML element, then configure // it for the event and the function to call when the event occurs. document.querySelector("button").addEventListener("click", showGrade); // JavaScript is case-sensitive. Functions use the word "function" (lower case) // and, in this case, need a name (that you can make up) to be able to call it later. function showGrade(){ var x = document.getElementById("score").value; // The condition for an "if" must be in parenthesis // and when there are multiple parts to the conditon // you have to use AND (&&) or OR (||) operators and // each part must be a complete condition on its own. // Also, the colons you had were incorrect syntax. // Lastly, because a grade will be only one of your // choices, you should use "else if" on the second and // subsequent tests, so that if the first test fails, // each of the next ones will run. But, once you've // ruled out all the possiblilites except for one (the // last one), you don't need to test and just use "else". // For example, if the grade isn't and A,B,C, or D, it // must be an F. if (x >=1 ){ alert("invalid grade") } else if (x >= .9 && x <= .99) { alert("A"); } else if (x >= .8 && x <= .89) { alert("B"); } else if (x >= .7 && x <=.79) { alert("C"); } else if (x >= .6 && x <= .69) { alert("D"); } else { alert("F"); } } </script> </body> </html> 

More reading:

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