...

View Full Version : Getting a NaN even after parseFloat()ing



ipruthi
07-30-2002, 01:39 AM
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.

adios
07-30-2002, 02:19 AM
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).....

BrainJar
07-30-2002, 03:54 PM
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;

ipruthi
07-30-2002, 07:15 PM
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

adios
07-30-2002, 07:31 PM
ipruthi...

Still :confused:


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);

adios
07-30-2002, 08:25 PM
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>

ipruthi
07-30-2002, 09:20 PM
Your modification worked superbly well!

Thank you for quick help:thumbsup:

I.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum