Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

Beginner needs help (parseInt). JS/HTML

I started to learn this coding thing over a week ago and I don't have much experience in it ( as you probably see).
I have no idea what I'm doing, therefore my code doesn't work, For some reason it prints only first 2 answers.
If you could just let me know how to use if...else statements while using parseInt(document.getElementById("speed").value), it would be great.
Thanks in advance.

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Penalty</title>
</head>

<body>

    <form>
        <label>Fine:</label>
        <input type="text" id="speed" size="3">
        <input type="button" value="Submit" onclick="sSubmit()">
    </form>

    <p id="answer"></p>

    <script type="text/javascript">

        function sSubmit() {
            var speed = parseInt(document.getElementById("speed").value);

            var result;

            if (speed <= 60) {
                result = " No fine";
            }
            else if (speed >= 61 || speed <= 71) {
                result = "Fine is 100€";
            } else if (speed >= 72 || speed <= 75) {
                result = "Fine is 170€";
            } else if (speed >= 76 || speed <= 80) {
                result = "Fine is 200€";
            } else {
                result = "Day-fine ";
            }

            document.getElementById("answer").innerHTML = "<p>" + result + "</p>";
        }

    </script>
</body>

</html>
question from:https://stackoverflow.com/questions/65847577/beginner-needs-help-parseint-js-html

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You probably should be using && (which means and), not || (which means or).

With your current code, one of the first two options are always going to be run because if the speed is greater than 60 (and thus makes it to the first else if), then it is always going to be either greater than 60 or less than 72. You want it to be greater than 60 and less than 72.

An updated example:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Penalty</title>
</head>
<body>

    <form>
        <label>Fine:</label> 
        <input type="text" id="speed" size="3"> 
        <input type="button" value="Submit" onclick="sSubmit()">
    </form>
    
    <p id="answer"></p>

    <script type="text/javascript">
          
   function sSubmit () {
       var speed = parseInt(document.getElementById("speed").value);
  
       var result;    
       
       if (speed <= 60){
      result = " No fine" ;
           }
            else if (speed >= 61 && speed <=71 ) {
                 result = "Fine is 100€" ;
                
                } else if (speed >= 72 && speed <=75 ) {
                 result = "Fine is 170€" ;
                    
                    } else if (speed >= 76 && speed <=80 ) {
                        result = "Fine is 200€" ;
        
                } else {
                    result = "Day-fine "; 
                    
            }
   
        document.getElementById("answer").innerHTML= "<p>" + result + "</p>";    
    } 
        
    </script>
</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...