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
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Getting a NaN even after parseFloat()ing

    Hi,

    I'm writing a function to which I'm sending 4 variables (2 floats, and 2 integers). That function is supposed to do a simple calculation and alert() the result to the user's browser. However, I keep getting "NaN" in the alert() window. I've tried parseFloat()ing but the problem persists. This is the last try I gave it:

    function calculateTicPrice(priceAdult, priceChild, travelAdult, travelChild)
    {
    if (document.firstForm.adults.value != null &&
    document.firstForm.adpricejs.value != null &&
    document.firstForm.child.value != null &&
    document.firstForm.chpricejs.value != null)
    {
    tempDateCalc1 = (document.firstForm.adpricejs.value) * (document.firstForm.adults.value);
    tempDateCalc1 = parseFloat(tempDateCalc1);
    tempDateCalc2 = (document.firstForm.chpricejs.value) * (document.firstForm.children.value);
    tempDateCalc2 = parseFloat(tempDateCalc2);

    finalDisp = tempDateCalc1 + tempDateCalc2;

    finalDisp = parseFloat(finalDisp);

    alert('Total: '+ finalDisp);
    }
    else
    {
    alert('Please enter number.');
    }
    }//End of function

    Anyone have any idea why it's returning a "NaN" and not the expected calculation? Please help.

    Thanks in advance.

    I.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Well...

    document.firstForm.child[ren]

    Typo?

    Also, empty form fields aren't null - they contain the empty (null) string. Easy to test for:

    if (document.firstForm.adults.value).....

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    185
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You need to test the value to make sure it can be parsed as a number. This code will set 'n' to zero if 's' is not a numeric string:

    var n = parseFloat(s);
    if (isNaN(n))
    n = 0;

  • #4
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy still problems :(

    Hi,

    Tried your suggestions, I still get an error. Here's the code again, but with more info. This is what's happening:

    - I'm giving this function 4 variables: adults, child, adpricejs, chpricejs, with INTEGER, INTEGER, FLOAT, FLOAT values, respectively.

    - I want to multiply adults with adpricejs, store them in a temporary variable.

    - I want to multiply the child with chpricejs, store them in a temporary variable.

    - I want to add these two and alert() the final total to the user.

    However, all the tests pass through but I get an alert() saying: "Total: NaN". Anyone have any idea why this is happening? I think the problem could be in my procedure here, but I'm not that experienced with JavaScript as to know what's wrong. If anyone could help, I'd be grateful!

    Thanks, the code follows.

    I.


    function calculateTicPrice()
    {
    var tempDateCalc1, tempDateCalc2, finalDisp;

    if (!isNaN(document.firstForm.adults.value) && !isNaN(document.firstForm.adpricejs.value) &&
    !isNaN(document.firstForm.child.value) && !isNaN(document.firstForm.chpricejs.value))
    {
    document.firstForm.adults.value = parseFloat(document.firstForm.adults.value);
    document.firstForm.child.value = parseFloat(document.firstForm.child.value);
    document.firstForm.adpricejs.value = parseFloat(document.firstForm.adpricejs.value);
    document.firstForm.chpricejs.value = parseFloat(document.firstForm.chpricejs.value);

    tempDateCalc1 = parseFloat(tempDateCalc1);
    tempDateCalc1 = (document.firstForm.adpricejs.value) * (document.firstForm.adults.value);
    tempDateCalc2 = parseFloat(tempDateCalc2);
    tempDateCalc2 = (document.firstForm.chpricejs.value) * (document.firstForm.children.value);

    finalDisp = tempDateCalc1 + tempDateCalc2;

    finalDisp = parseFloat(finalDisp);

    alert('Total: '+ finalDisp);
    }
    else
    {
    alert('Please enter number.');
    }

    }//ID: 8

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    ipruthi...

    Still

    I'm giving this function 4 variables: adults, child, adpricejs, chpricejs
    These:

    document.firstForm.adults.value
    document.firstForm.child.value
    document.firstForm.adpricejs.value
    document.firstForm.chpricejs.value

    ...are clearly references to form elements - so:

    document.firstForm.children.value

    ...would appear to be a typo.

    Oops...got these backward, too:

    tempDateCalc1 = parseFloat(tempDateCalc1);
    tempDateCalc1 = (document.firstForm.adpricejs.value) * (document.firstForm.adults.value);
    tempDateCalc2 = parseFloat(tempDateCalc2);
    tempDateCalc2 = (document.firstForm.chpricejs.value) * (document.firstForm.children.value);
    Last edited by adios; 07-30-2002 at 09:00 PM.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Here's a suggestion; I'm assuming the per-ticket prices are being written to hidden form fields.


    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript" language="JavaScript">

    function calculateTicPrice() {
    var tempDateCalc1, tempDateCalc2, finalDisp, el;
    el = document.firstForm.adults;
    if (!el.value || isNaN(el.value)) {
    alert('Please enter a valid number of adults.');
    el.focus(); el.select(); return;
    }
    el = document.firstForm.child;
    if (!el.value || isNaN(el.value)) {
    alert('Please enter a valid number of children.');
    el.focus(); el.select(); return;
    }
    tempDateCalc1 = parseFloat(document.firstForm.adpricejs.value * document.firstForm.adults.value);
    tempDateCalc2 = parseFloat(document.firstForm.chpricejs.value * document.firstForm.child.value);
    finalDisp = (tempDateCalc1 + tempDateCalc2).toString();
    if (finalDisp.indexOf('.') == -1) finalDisp += '.00';
    else if (finalDisp.charAt(finalDisp.length-2) == '.') finalDisp += '0';
    document.firstForm.total.value = '$' + finalDisp;
    }

    </script>
    </head>
    <body>
    <form name="firstForm">
    <input type="text" name="adults" size="8">___# of adults<br>
    <input type="text" name="child" size="8">___# of children<br>
    <input type="text" name="total" size="8" readonly="readonly">______total price<br><br>
    <input type="button" value="Get Total Cost" onclick="calculateTicPrice()">
    <input type="hidden" name="adpricejs" value="25.99">
    <input type="hidden" name="chpricejs" value="12.49">
    </form>
    </body>
    </html>

  • #7
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Worked like a charm!

    Your modification worked superbly well!

    Thank you for quick help

    I.


  •  

    Posting Permissions

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