PDA

View Full Version : NaN error when dividing Math.round

teama
03-29-2012, 06:09 AM
Having trouble with the alert(Math.round coding. "r" is the results or final value, and I want to divide this result/final value by "f"

I have alert(Math.round(r / f) + ' calories/day to burn); but I its coming up saying NaN calories/day to burn.

How do I divide "f" into "r" correctly?

}
function calculate() {
var f = document.forms[0], g, w, f, h, a, l, r;
for (var i=0; i<(f.length-1); i++) {
if (f.elements[i].selectedIndex == 0) {
alert('Sorry, all options must be selected.');
return false;
}
}
g = f.g.options[f.g.selectedIndex].value;
w = f.w.options[f.w.selectedIndex].value;
h = f.h.options[f.h.selectedIndex].value;
a = f.a.options[f.a.selectedIndex].value;
l = f.l.options[f.l.selectedIndex].value;
if (g == 'm') {
r = ((66 + ((13.7 * w) / 2.2) + (5 * h * 2.54)) - (6.8 * a)) * l;
} else {
r = ((655 + ((9.6 * w) / 2.2) + (1.8 * h * 2.54)) - (4.7 * a)) * l;
}
alert(Math.round(r) + ' calories/day is what you require');
alert(Math.round(r / f) + ' calories/day to burn);
return false;

Dormilich
03-29-2012, 07:09 AM
you can’t divide through a form, only through a number.

Nile
03-29-2012, 07:16 AM
Use parseInt() before you try to divide strings.

Old Pedant
03-29-2012, 07:39 AM
What Dormilich was telling you is that your variable f is the ENTIRE FORM.

See here:

var f = document.forms[0]

Somehow you got mixed up and used the wrong variable.

*********

Nile wrote: >> Use parseInt() before you try to divide strings.

Ummm...and what about if the numbers aren't integers?

Actually, the only time you need to use parseInt/parseFloat/Number is when you are using the addition oparate (+_. JavaScript will happily convert strings to number for you, automatically, for all other operations. Go ahead, try it:

alert( "17" / "3" );

Old Pedant
03-29-2012, 07:42 AM
And by the by, all your lines that do this:

g = f.g.options[f.g.selectedIndex].value;

can be replaced with simply

g = f.g.value;

The need to use selectedIndex like that disappeared when Netscape 4.7 bit the dust about 11 years ago.

felgall
03-29-2012, 10:06 AM
Use parseInt() before you try to divide strings.

The shorter way to convert a string to a number is to use +() and the longer way is to use Number()

for example +('1') + 1 will correctly give 2.

parseInt requires that you start with a number in a base between 2 and 36 and it will convert that number to base 10 for you. The number base you are converting from goes in the second parameter eg parseInt('y',36) will return 34. If you don't specify the base then numbers starting with 0x are assumed to be base 16 and if you don't use strict mode numbers starting with 0 might be treated as base 8. It is NOT for converting strings to numbers at all.

parseFloat is useful for handling very big and very small numbers eg parseFloat('1e100') will return 1 googol.

Dormilich
03-29-2012, 10:11 AM
I may point out that the string-number conversion is not the problem of the OP (since there is no numeric string in combination with the + operator).

Philip M
03-29-2012, 10:59 AM
The shorter way to convert a string to a number is to use +() and the longer way is to use Number()

for example +('1') + 1 will correctly give 2.

Far better and less ambiguous is to multiply the string value by 1 (which has always been my preferred method although Old Pedant has called it a hack).

Number(whatever) || 0 has the merit of changing a NaN value to 0 (zero). I often use it.

Mishu
03-29-2012, 11:38 AM
How do I divide "f" into "r" correctly?

}
function calculate() {
var f = document.forms[0], g, w, f, h, a, l, r;
for (var i=0; i<(f.length-1); i++) {
if (f.elements[i].selectedIndex == 0) {
alert('Sorry, all options must be selected.');
return false;
}
}
g = f.g.options[f.g.selectedIndex].value;
w = f.w.options[f.w.selectedIndex].value;
h = f.h.options[f.h.selectedIndex].value;
a = f.a.options[f.a.selectedIndex].value;
l = f.l.options[f.l.selectedIndex].value;
if (g == 'm') {
r = ((66 + ((13.7 * w) / 2.2) + (5 * h * 2.54)) - (6.8 * a)) * l;
} else {
r = ((655 + ((9.6 * w) / 2.2) + (1.8 * h * 2.54)) - (4.7 * a)) * l;
}
alert(Math.round(r) + ' calories/day is what you require');
alert(Math.round(r / f) + ' calories/day to burn);
return false;

your f is an object reference to your form and not a number unless I've missed something.

I normally convert string to numbers using Number() and then have appropriate error handling if it retirns NaN like either output an error message or set to whatvere is the appropriate default for the variable.

Philip M
03-29-2012, 11:41 AM
your f is an object reference to your form and not a number unless I've missed something.

I normally convert string to numbers using Number() and then have appropriate error handling if it retirns NaN like either output an error message or set to whatvere is the appropriate default for the variable.

Is this not wasteful and unnecessary duplication? What does it add to what has already been stated by others?

Mishu
03-29-2012, 11:44 AM
What does it add to what has already been stated by others?

1. I actually point out for the op where in their code there is an issue.

2. That I have appropriate error handling and I don't hard code a value of 0 if NaN is returned.

But isn't your querying my post a waste in itself and an attemp to hijack this thread? Why don't you just report my post to the mods and they can delete it if they agree with you :)

Dormilich
03-29-2012, 11:48 AM
all this discussion is pointless by now until teama tells us, what the quotient r / f is supposed to mean.

@Mishu: you are aware that number conversion doesn’t play any role in the code?

Mishu
03-29-2012, 11:51 AM
all this discussion is pointless by now until teama tells us, what the quotient r / f is supposed to mean.

@Mishu: you are aware that number conversion doesn’t play any role in the code?

Yes I am aware just as those who also posted on number conversion are most probably aware as well.