SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    throwing exceptions

    I have been working on this simple script but I have a problem take a look!
    Code:
    <html>
    <head>
    <script>
    function calculate(){
     	val_1=parseInt(document.exp.int1.value);
     	val_2=parseInt(document.exp.int1.value);
    	try{
    		if(isNaN(val_1)||isNaN(val_2)){
    		throw "Enter Only Numbers Please?";
    		}
    		else{
    		ans=val_1+val_2;
    		throw "The Answer is: " +ans;
    		}
    	}
    	catch(e){
    		alert(e);
    	}
    }
    
    </script>
    
    </head>
    <body>
    <form name="exp">
    <input type="text" name="int1">+
    <input type="text" name="int2">
    <input type="button" value="Calculate" onclick="calculate()">
    </form>
    </body>
    </html>
    Looks Fine! Nope when I add something simple say (8+6) it results in 16 thats not right of course! I probly just need to set radix to correct setting but I dont know what?

  2. #2
    SitePoint Addict xDev's Avatar
    Join Date
    Jul 2003
    Location
    Moncton, New Brunswick, Canada
    Posts
    247
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Your the same value to both variables.

    Code:
    function calculate(){
     	val_1=parseInt(document.exp.int1.value);
     	val_2=parseInt(document.exp.int1.value);
    	try{
    Code:
    function calculate(){
     	val_1=parseInt(document.exp.int1.value);
     	val_2=parseInt(document.exp.int2.value);
    	try{
    -xDev

  3. #3
    SitePoint Wizard silver trophy Crazybanana's Avatar
    Join Date
    Mar 2003
    Location
    In tha fruit cellar
    Posts
    1,379
    Mentioned
    32 Post(s)
    Tagged
    1 Thread(s)
    yep, and then it is 8+8 and that is 16. try what xDev said.
    Who's to doom when the judge himself is dragged before the bar



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •