Re: Check null or 0
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: Check null or 0

From: VK <schools_ring@yahoo.com>
Date: Tue Feb 28 2006 - 10:21:01 CET

staeri@gmail.com wrote:
> I'm using the following code to create a sum:
>
> forecast = forecast + eval(f[i].value);
>
> This only works if "f[i].value" contains a number.
>
> Can someone please help me so that it also works if "f[i].value" is
> empty or contains text.

forecast += +(f[i].value)||0;

P.S. I decided to participate in the squeezcrypt rally :-) I did not
play squeezcrypt for rather long so just cheking my fingers are stll
flexible :-)

The decrypter version (if someone curious) is:

var n = parseInt(f[i].value, 10);
if (isNaN(n)) {
 forecast += 0;
}
else {
 forecast += n;
}
Received on Mon May 1 03:40:58 2006