JRS: In article <11**********************@o13g2000cwo.googlegroups .com>
, dated Sun, 7 Aug 2005 09:42:40, seen in news:comp.lang.javascript,
pa****************@hotmail.com posted :
So I have a table setup like this:
<table>
<tr>
<td>425</td>
</tr>
</table>
and I'm retrieving a value out of a table using this code:
var x=document.getElementById('myTable').rows
var y=x[0].cells
var usedMin = y[0].innerHTML;
now, I know that usedMin is a number, when I call:
return usedMin
I get "425". (the number I expect to get.) But for some reason, if I
try to do any math on it, like:
usedMin - 100
parseInt(usedMin)
I get the NaN error. Does anyone know why I can't type cast this as an
integer?
NaN is not, of itself, an error. Either it is one of the possible
values of a javascript Number, or it is one of an indistinguishable set
of such values (other languages can distinguish members of the set).
One reason why you cannot typecast usedMin as an integer is that current
javascript has no such type as integer. You should be able to use
Number(usedMin) though. Note : parseInt() is not a typecast.
Your prime error seems to be that you have not studied the newsgroup
FAQ.
Using parseInt is generally unnecessary (use it if there may be other
characters after the numeric part); and, when it is used, it should
generally be used with a second parameter, commonly 10.
To test whether usedMin is a Number, use alert(typeof usedMin).
If it is certain that the contents of innerHTML will be a numeric
(decimal) string (it will of course be of type String), then all you
should need is return +usedMin . Otherwise, validate (see sig
line 3) or be prepared for NaN.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.