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 7 of 7
  1. #1
    Senior Coder
    Join Date
    May 2004
    Posts
    1,457
    Thanks
    15
    Thanked 0 Times in 0 Posts

    concatenate added numbers

    i'm trying to concatenate to a string 2 variables that i'm adding together but i'm getting Nan as the result

    what am i doing wrong?


    str_amount=parseInt(document.getElementById('amount').value);
    strfeeamount=parseInt(document.getElementById('feeamount').value);

    urltoajax=urltoajax + '&amount=' +str_amount +strfeeamount

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,286
    Thanks
    12
    Thanked 344 Times in 340 Posts
    because you parse the strings as Numbers (parseInt()) which returns NaN.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Senior Coder
    Join Date
    May 2004
    Posts
    1,457
    Thanks
    15
    Thanked 0 Times in 0 Posts
    but stramount and strfee are numbers that I want to add together and then concatenate to the string

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,286
    Thanks
    12
    Thanked 344 Times in 340 Posts
    from the result you get, I only can assume, they’re not valid numbers.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Senior Coder
    Join Date
    May 2004
    Posts
    1,457
    Thanks
    15
    Thanked 0 Times in 0 Posts
    they are both numbers

    1 is 515 and the other is 90

    i added an alert and it shows the numbers correctly - it's only wrong when i try concatenating them to the string

    please advise

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,286
    Thanks
    12
    Thanked 344 Times in 340 Posts
    well, are there any other things you do then? because I don’t have that issue. maybe you could post a link to that page, so that I can verify that problem myself.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,518
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    Why do you name your variables str_amount and strfeeamount when, in fact, they are *NOT* strings and CAN NOT be???

    When you use parseInt as you did, they *WILL* be integers--or they will be NaN. But they won't be strings.

    Code:
    var Amount = parseInt(document.getElementById('amount').value);
    var Feeamount = parseInt(document.getElementById('feeamount').value);
    
    if ( isNaN(Amount) || isNaN(Feeamount) )
    {
        alert("Invalid:  one of the amounts is not a number");
    } else {
        urltoajax += '&amount=' + ( Amount + Feeamount );
    }
    Notice the parens around the sum of the numbers: Without them, you'd be doing string concatenation because you would be appending to the string '&amount='.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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