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

    Join Date
    Mar 2013
    Rep Power

    Concatenation vs addition, help appreciated


    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;

    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
    Rep Power
    Using parseInt is the correct approach.

    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