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: Evertjan. <exjxw.hannivoort@interxnl.net>
Date: Mon Feb 27 2006 - 19:50:29 CET

 wrote on 27 feb 2006 in comp.lang.javascript:

> 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.

eval() is evil, and I see no reason why you are using it here.

===========

If you mean the following:

var forecast = 7
var xxx = 8
var yyy ='xxx'
forecast = forecast + eval(yyy);
alert(forecast) //15

it is much better to use:

var forecast = 7
var xxx = 8
var yyy ='xxx'
forecast = forecast + window[yyy];
alert(forecast) //15

now what if xxx is not initialized or NaN:

var forecast = 7
var xxx
var yyy = 'xxx'
if (!window[yyy]||isNaN(window[yyy])) {
        var a = 0
        yyy = 'a'
}
forecast = forecast + window[yyy];
alert(forecast) //7

will this help?

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Received on Mon May 1 03:40:00 2006