Could you pls check this code?

javascript Estonia
  • 14 years ago
    I cant understand whats the problem with it... Im trying to make a button, which upon being pressed pops up a prompt box asking for a digit from 0 to 10, which then upon returning the value will display the user an alert telling he either succeeded in entering the correct number or not.



    <html>
    <head>
    <script type="text/javascript">

    var x=prompt("Enter a number between 0 and 10:","")

    function throw()
    {
    try
       {
       if(x>10)
         throw "Err1"
       else if(x<0)
         throw "Err2"
       else if(isNaN(x))
         throw "Err3"
       else if(0 < x && x < 10)
         throw "Err4"
       }
    catch(er)
       {
       if(er == "Err1")
         alert("Error! The value is too high")
       if(er == "Err2")
         alert("Error! The value is too low")
       if(er == "Err3")
         alert("Error! The value is not a number")
       if(er == "Err4")
        alert("You entered a valid number! Way to go..")
       }
    }
    </script>
    </head>
    <body>

    <input type="button" value="Enter Digit" onclick="throw()" />

    </body>
    </html>











































  • 14 years ago

    Move the

    var x=prompt("Enter a number between 0 and 10:","")

    inside the throw() function, before the try.

    By the way, you don't need to use a try/catch here - you can just put the alert inside the if statements, like so:

    if( x > 10 )
    {
        alert( "Too high!" );
    }


    As an endnote, I had no idea what your problem was so I had to guess. You should elaborate so that we can help you better.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.” - Dennis Ritchie