0

Y this code doesn't work with me? , i did all steps , so any one plz tell me where is my fault , and thank u

<html>
<body>
<script>
function myFunction()
 {
    try
    {
        var x=document.getElementById("demo").value;
        if(x=="")    throw="empty";
        if(isNaN(x)) throw"not a number";
        if(x>10)     throw"too high";
        if(x<5)      throw"too low";
    }
    catch(err)
    {
        var y=document.getElementById("mess");
        y.innerHTML="Error:" + err + ".";
    }
}
</script>
<h1>My First JavaScript</h1>
<p>Please input a number between 5 and 10:</p>
<input id="demo" type="text" />
<button type="button" onclick="myFunction()">Test input</button>
<p id="mess"></p>
</body>
</html>

2 Answers 2

2

It seems you have included an = incorrectly in your throw statement here,

 if(x=="")    throw="empty";

just remove it and try.

Here is the working fiddle: https://jsfiddle.net/74suvvfq/

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0
$("button").click(function() {
  try {
    var x=document.getElementById("demo").value;
    if(x=="")    throw "empty";
    if(isNaN(x)) throw  "not a number";
    if(x>10)     throw "too high";
    if(x<5)      throw "too low";
 }
 catch(err) {
    var y=document.getElementById("mess");
    y.innerHTML="Error:" + err + ".";
}
});

Hope this will help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.