[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Can i compare with an empty array element?

JT

3/11/2016 7:58:00 AM

function untangle_links(arr)
{
printnode = new Array;
var j = 0;
var tempnode = 0;
var tnode = new Array();
for(var i = 0; i < nodes; i ++ )
{
for(var k = 0; k < tnode.length; k ++ )
{
if (arr[tempnode].nodelinks[j] == tnode[k])//Is this ok?
{ //tnode[k] undefined or NaN?
j ++ ;
}
}
tempnode = arr[tempnode].nodelinks[j];
tnode[tnode.length] = tempnode;
printnode[i] = tempnode;
j = 0;
}
return printnode;
}
2 Answers

JT

3/11/2016 8:16:00 AM

0

Den fredag 11 mars 2016 kl. 08:58:27 UTC+1 skrev jonas.t...@gmail.com:
> function untangle_links(arr)
> {
> printnode = new Array;
> var j = 0;
> var tempnode = 0;
> var tnode = new Array();
> for(var i = 0; i < nodes; i ++ )
> {
> for(var k = 0; k < tnode.length; k ++ )
> {
> if (arr[tempnode].nodelinks[j] == tnode[k])//Is this ok?
> { //tnode[k] undefined or NaN?
> j ++ ;
> }
> }
> tempnode = arr[tempnode].nodelinks[j];
> tnode[tnode.length] = tempnode;
> printnode[i] = tempnode;
> j = 0;
> }
> return printnode;
> }

I do not think i should need a special case to compare with null, undefined or NaN. But you never know. I mean if it isn't the same it isn't.

Simon Blackwell

3/14/2016 1:00:00 PM

0

On Friday, March 11, 2016 at 3:16:26 AM UTC-5, jonas.t...@gmail.com wrote:
> Den fredag 11 mars 2016 kl. 08:58:27 UTC+1 skrev jonas.t...@gmail.com:
> > function untangle_links(arr)
> > {
> > printnode = new Array;
> > var j = 0;
> > var tempnode = 0;
> > var tnode = new Array();
> > for(var i = 0; i < nodes; i ++ )
> > {
> > for(var k = 0; k < tnode.length; k ++ )
> > {
> > if (arr[tempnode].nodelinks[j] == tnode[k])//Is this ok?
> > { //tnode[k] undefined or NaN?
> > j ++ ;
> > }
> > }
> > tempnode = arr[tempnode].nodelinks[j];
> > tnode[tnode.length] = tempnode;
> > printnode[i] = tempnode;
> > j = 0;
> > }
> > return printnode;
> > }
>
> I do not think i should need a special case to compare with null, undefined or NaN. But you never know. I mean if it isn't the same it isn't.

I think you will need:

arr[tempnode].nodelinks[j] == tnode[k] || (typeof(arr[tempnode].nodelinks[j])==="number" && typeof(tnode[k])==="number" && isNaN(
arr[tempnode].nodelinks[j]) && isNaN(tnode[k]))

According to the JavaScript spec a NaN does not equal a NaN, probably because they could represent different irrational numbers. Regardless, isNaN provides a way to check.