Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    pgb
    pgb is offline
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    simple calculator, + doesn't work

    With this simple calculator + doesn't work, it puts numbers together instead of adding.
    * / - all work fine

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    
    <html>
    <head>
    <title>Calculate</title>
    </head>
    <body>
    <script type="text/javascript" language="javascript">
    <!--
    
    function checkform() {
    
    	var num1=document.addnumbers.num1.value; //get the first number from the form send it to a variable called num1
    	
    	var num2=document.addnumbers.num2.value; //get the second number from the form send it to a variable called num2
    	
    	function calculate (a,b) //create a function called calculate that will do a + b = x
    	{x = a + b;														//this works fine with *, /, and -, but not with +, which it treats like a string!
    	return x;}
    	
    	var total=calculate(num1,num2) //create a variable called total that will use the calculate function on num1 and num2
    
    	alert ("The total is " + total) //alert box will display total when form submitted
    
    }
    
    -->
    </script>
    
     
    <form onsubmit="return checkform()" name="addnumbers" action="" method="post">
    
    
    Number 1<input type="text" name="num1"><br />
    Number 2 <input type="text" name="num2"><br />
    
    
    
    <input type="submit" class="button" value="Get the total!" />
    
    
     
    </form>
    
    
    </body>
    </html>
    Last edited by pgb; 06-30-2006 at 10:38 AM.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Thats because the values are returned as strings. You can use Number(), parseInt() or parseFloat(). I prefer Number() since it can handle ints and floats
    Code:
    	var num1=Number(document.addnumbers.num1.value); //get the first number from the form send it to a variable called num1
    	
    	var num2=Number(document.addnumbers.num2.value); //get the second number from the form send it to a variable called num2
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New Coder
    Join Date
    Jun 2006
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    String to Integer

    Its simply a conversion to Integer that needs to happen here.

    function calculate (a,b)
    {
    x = (a-0) + (b-0); return x;
    }

  • #4
    pgb
    pgb is offline
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks!


  •  

    Posting Permissions

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