Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Check null or 0

Reply
Thread Tools

Check null or 0

 
 
staeri@gmail.com
Guest
Posts: n/a
 
      02-27-2006
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.

Regards,

S

 
Reply With Quote
 
 
 
 
Matt Kruse
Guest
Posts: n/a
 
      02-27-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I'm using the following code to create a sum:
> forecast = forecast + eval(f[i].value);


Don't use eval.
http://www.JavascriptToolbox.com/bestpractices/

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


Well, what do you want it to do?
Should it treat non-number values as 0?
Ignore them?
Throw up an error?

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


 
Reply With Quote
 
 
 
 
jshanman
Guest
Posts: n/a
 
      02-27-2006
(E-Mail Removed) 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.
>
> Regards,
>
> S


try this:

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

http://www.w3schools.com/jsref/jsref_parseInt.asp

- JS

 
Reply With Quote
 
web.dev
Guest
Posts: n/a
 
      02-27-2006

(E-Mail Removed) wrote:
> I'm using the following code to create a sum:
>
> forecast = forecast + eval(f[i].value);


The use of eval is most often misused and unnecessary.

Try the following to create your sum:

forecast += +f[i].value;

 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      02-27-2006
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)
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      02-28-2006
(E-Mail Removed) said the following on 2/27/2006 1:30 PM:
> 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.


Simple. Drop the eval:

forecast = forecast + f[i].value;

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      02-28-2006

(E-Mail Removed) 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;
}

 
Reply With Quote
 
Jonas Raoni
Guest
Posts: n/a
 
      02-28-2006
VK wrote:
> forecast += +(f[i].value)||0;


There's no need for the parenthesis.

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


I saw a lot of people criticizing you here, I thought they just hated
you, but after reading this message I can see they are totally right
hahaha ;]


--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      02-28-2006

Jonas Raoni wrote:
> VK wrote:
> > forecast += +(f[i].value)||0;

>
> There's no need for the parenthesis.


Right.

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

As I said I did not play squeezcrypt for a while


> I saw a lot of people criticizing you here, I thought they just hated
> you, but after reading this message I can see they are totally right
> hahaha ;]


Oh boy, did we get hurt?
I thought there were enough of smilies in my post to prevent that -
plus nothing personal.

So what the exact reason of *your* hate is? That I explained what did
forecast+=+f[i].value||0;
do and mean?

It's hard to believe, I know, but it is not so obvious at all to an
average user. Plus some people may prefer to use the conventional
isNaN() check so I thought necessary to mention it.

 
Reply With Quote
 
Jonas Raoni
Guest
Posts: n/a
 
      02-28-2006
VK wrote:
> Jonas Raoni wrote:
>>I saw a lot of people criticizing you here, I thought they just hated
>>you, but after reading this message I can see they are totally right
>>hahaha ;]

>
> Oh boy, did we get hurt?


Hahahaha, no! We'll never get hurt, this isn't an arena, but a place to
spend some free time, I don't take anything personally ;]

Even disliking such unuseful discussions, I can't resist to my agressive
instinct, so if you can, stop this thread by not answering, it's already
over, the question got an answer =]

> So what the exact reason of *your* hate is? That I explained what did
> forecast+=+f[i].value||0;
> do and mean?


Yeah, that's the reason, explaining how the script works looks idiot for
me, I explain only if the person asks for explanation.

> It's hard to believe, I know, but it is not so obvious at all to an
> average user. Plus some people may prefer to use the conventional
> isNaN() check so I thought necessary to mention it.


You're right, but explaining the script is a kind of insult for the
idiot people like me haha. I assume everybody has a nice knowledge about
the language, so nothing needs explanation.


--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To check or not to check for NULL? Mike Java 12 09-26-2008 10:46 PM
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
VB.NET Null to SQL Null (ASP.NET 2.0 GridView) Kivak Wolf ASP .Net 2 06-28-2005 02:01 PM
is (!ptr) or (ptr) valid way to check for NULL or NOT NULL? G Fernandes C Programming 9 02-27-2005 03:07 AM



Advertisments