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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Problem with addition

    Hi,
    I'm just learning JS by going through a book, while trying one of the examples in the book I ran into a problem with " + " operator. It think it's best I show by example (don't worry it's a fairly short script):

    Script
    Code:
    /* This is an attempt to do some math with javascript */
    var a;
    var b; 
    var c;
    
    a = prompt("Pick your favorite number");
    b = prompt("Pick your second favorite number");
    c = a + b
    
    
    	document.write("a = " + a + ", b = " + b + ", c = " + c + "<br />");
    	
    	document.write("<br />a + b = " + c +  "<br />");
    	c += 6
    	
    	document.write("<br />c + 6 = " + c + "<br />");
    	c -= 5
    	
    	document.write("<br />c - 5 = " + c + "<br />");
    	c *= 3
    	
    	document.write("<br />c * 3 =" + c + "<br />");
    	c /= 2
    	
    	document.write("<br />c / 2 = " + c + "<br />");
    	c %= 7
    	
    	document.write("<br />c % 7 = " + c + "<br />");
    	
    if (c > 0) { 
    		document.write("<br /> Wrong!"); 
    		}
    	else {
    		document.write("<br /> You, I like")
    		}
    Output

    a = 3, b = 4, c = 34

    a + b = 34

    c + 6 = 346

    c - 5 = 341

    c * 3 =1023

    c / 2 = 511.5

    c % 7 = 0.5

    Wrong!
    As you can see the script doesn't execute the mathematical operation for addition. It instead strings it together. What am I doing wrong?

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by errantwriter View Post
    Hi,
    I'm just learning JS by going through a book, while trying one of the examples in the book I ran into a problem with " + " operator. It think it's best I show by example (don't worry it's a fairly short script):

    Script
    Code:
    /* This is an attempt to do some math with javascript */
    var a;
    var b; 
    var c;
    
    a = prompt("Pick your favorite number");
    b = prompt("Pick your second favorite number");
    c = a + b
    
    
    	document.write("a = " + a + ", b = " + b + ", c = " + c + "<br />");
    	
    	document.write("<br />a + b = " + c +  "<br />");
    	c += 6
    	
    	document.write("<br />c + 6 = " + c + "<br />");
    	c -= 5
    	
    	document.write("<br />c - 5 = " + c + "<br />");
    	c *= 3
    	
    	document.write("<br />c * 3 =" + c + "<br />");
    	c /= 2
    	
    	document.write("<br />c / 2 = " + c + "<br />");
    	c %= 7
    	
    	document.write("<br />c % 7 = " + c + "<br />");
    	
    if (c > 0) { 
    		document.write("<br /> Wrong!"); 
    		}
    	else {
    		document.write("<br /> You, I like")
    		}
    Output



    As you can see the script doesn't execute the mathematical operation for addition. It instead strings it together. What am I doing wrong?
    + do both, string concatanation and addition based on the type of the arguments as you already guess,
    you need to be sure that type of a and b is number and not string. one way is to use parseInt, some people use to multiply with 1.
    Code:
    c = parseInt(a) + parseInt(b);
    c = 1*a + 1*b;
    best regards

  • Users who have thanked oesxyl for this post:

    errantwriter (12-29-2010)

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank You very much!

  • #4
    Regular Coder
    Join Date
    Dec 2010
    Location
    Sheffield, UK
    Posts
    138
    Thanks
    81
    Thanked 1 Time in 1 Post
    Quote Originally Posted by errantwriter View Post
    Hi,
    I'm just learning JS by going through a book, while trying one of the examples in the book I ran into a problem with " + " operator. It think it's best I show by example (don't worry it's a fairly short script)
    Out of interest, what book on Javascript are you going through? Is it any good/useful for beginners?
    Last edited by Hashim1; 12-29-2010 at 03:25 PM.

  • #5
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by Hashim1 View Post
    Out of interest, what book on Javascript are you going through? Is it any good/useful for beginners?
    I have a few Javascript books and I rarely use them. It's easier to search the web. Most search hits have explanations, code snippets and other useful stuff.

    And you can't cut-n-paste from a book into your code editor!


  •  

    Posting Permissions

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