samedi 7 avril 2018

Function if with different values for two buttons. Javascript

I need do two buttons with functions if. 1) First condition is fulfilled. If enter number 2 — true, if number<2 and if number>2 —INCORRECTLY. 2) Second condition must be If enter number 3 — true, if number<3 and if number>3 —INCORRECTLY. But second condition is not fulfilled. How prescribe functions in order to conditions fulfill both ?

<!DOCTYPE html>
<html>
<body>
<p>ENTER A NUMBER:</p>
<input id="demo" type="text">
<button type="button" onclick="myFunction()">VERIFY</button>
<p id="message"></p>
<script>
function myFunction() {
    var message, x;
    message = document.getElementById("message");
    message.innerHTML = "";
    x = document.getElementById("demo").value;
            try { 
        if(x == "2")  throw "TRUE !"  ;
                if(isNaN(x)) throw "not a number";
        x = Number(x);
                if(x > 2)    throw "INCORRECTLY";
                if(x < 2)   throw "INCORRECTLY";  }
        catch(err) { message.innerHTML = "ANSWER " + err ;    }}
</script>
</body>
</html>
<body>
<p>ENTER A NUMBER:</p>
<input id="demo" type="text">
<button type="buttonA" onclick="myFunctionA()">VERIFY</button>
<p id="message"></p>
<script>
function myFunctionA() {
    var message, x;
    message = document.getElementById("message");
    message.innerHTML = "";
    x = document.getElementById("demo").value;
            try { 
        if(x == "3")  throw "TRUE !"  ;
                if(isNaN(x)) throw "not a number";
        x = Number(x);
                if(x > 3)    throw "INCORRECTLY";
                if(x < 3)   throw "INCORRECTLY";  }
        catch(err) { message.innerHTML = "ANSWER " + err ;    }}

</script>
</body>
</table>
</html>

enter image description here

Aucun commentaire:

Enregistrer un commentaire