#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Concatenation vs addition, help appreciated


    Hey

    I'm taking a beginner class and an assignment I was given was to create an equation with JavaScript that web viewers could use with whatever inputs they chose. Here's the source code:

    javascript Code:
     
    var a;
    var b;
    var c;
    a = prompt("a =");
    b = prompt("b =");
    c = prompt("c =");
    var result;
    result = a + b + c;
    alert(result);


    My problem is that

    javascript Code:
     
    result = a + b + c;


    concatenates the values instead of adding them; I searched and found that applying parseInt(variable) fixes this and tried it with success, but I was wondering if there is a simpler way, a function that performs "result = a + b + c" with addition rather than concatenation. I've tried various incarnations of "result a + b + c" and haven't been able to overcome this.

    I appreciate any advice and thank you for reading this post. I also apologize if this question has been broached before.

    Thank you
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    Using parseInt is the correct approach.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo