[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.javascript

How to manipulate integer that sometimes Nan and sometimes have a value?

JT

9/25/2015 2:31:00 PM

This is how i chosen to do it, is there a better way?

if (isNaN(addOne[i])) addOne[i] = 0; addOne[i] = addOne[i]+1;

43 Answers

Evertjan.

9/25/2015 3:09:00 PM

0

jonas.thornvall@gmail.com wrote on 25 Sep 2015 in comp.lang.javascript:

> This is how i chosen to do it,

To do what?

> is there a better way?

I am reasonably sure there is,
but that depends on the answer of the first Q.

> if (isNaN(addOne[i])) addOne[i] = 0; addOne[i] = addOne[i]+1;

This would do the same,
[using kiss, the array seems not to be a part of your Q]:

if (isNaN(z))
z = 0;
z++;

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

JT

9/25/2015 9:43:00 PM

0

Den fredag 25 september 2015 kl. 17:09:22 UTC+2 skrev Evertjan.:
> jonas.thornvall@gmail.com wrote on 25 Sep 2015 in comp.lang.javascript:
>
> > This is how i chosen to do it,
>
> To do what?
>
> > is there a better way?
>
> I am reasonably sure there is,
> but that depends on the answer of the first Q.
>
> > if (isNaN(addOne[i])) addOne[i] = 0; addOne[i] = addOne[i]+1;
>
> This would do the same,
> [using kiss, the array seems not to be a part of your Q]:
>
> if (isNaN(z))
> z = 0;
> z++;
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)

Well that is basicly the same?
if (isNaN(addOne[i])) addOne[i]=0;
addOne[i++] //Is that one even ok?

Danny

9/25/2015 10:11:00 PM

0

to jonas.t

in short, if the value is an integer, it will never return NaN.
What if may be happening in your case, is that the object gets
something else other than a numeric value.

but a solution depends on what the context for all that is
and you haven't given us context or relevant code of the matter

JT

9/26/2015 12:51:00 AM

0

Den lördag 26 september 2015 kl. 00:11:31 UTC+2 skrev Danny:
> to jonas.t
>
> in short, if the value is an integer, it will never return NaN.
> What if may be happening in your case, is that the object gets
> something else other than a numeric value.
>
> but a solution depends on what the context for all that is
> and you haven't given us context or relevant code of the matter

Well sometimes the index of the array is empty for the leading position, it is an carry operation.

Evertjan.

9/26/2015 6:55:00 AM

0

jonas.thornvall@gmail.com wrote on 25 Sep 2015 in comp.lang.javascript:

> Den fredag 25 september 2015 kl. 17:09:22 UTC+2 skrev Evertjan.:
>> jonas.thornvall@gmail.com wrote on 25 Sep 2015 in comp.lang.javascript:
>>
>> > This is how i chosen to do it,
>>
>> To do what?
>>
>> > is there a better way?
>>
>> I am reasonably sure there is,
>> but that depends on the answer of the first Q.
>>
>> > if (isNaN(addOne[i])) addOne[i] = 0; addOne[i] = addOne[i]+1;
>>
>> This would do the same,
>> [using kiss, the array seems not to be a part of your Q]:
>>
>> if (isNaN(z))
>> z = 0;
>> z++;
>
> Well that is basicly the same?

Only if you had responded affirmative on my first Q.

"Basically" all Javascript is "the same"
as it is just code doing something.

> if (isNaN(addOne[i])) addOne[i]=0;
> addOne[i++] //Is that one even ok?

addOne is a strange name for an array!

The statement:

addOne[i++];

will do NOTHING however and is equivalent to:

;

Please think before you post something.





--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Evertjan.

9/26/2015 7:00:00 AM

0

jonas.thornvall@gmail.com wrote on 26 Sep 2015 in comp.lang.javascript:

> Well sometimes the index of the array is empty for the leading position,
> it is an carry operation.

An index of an array CANNOT be empty,
because then it would not be an index.

Till you are in a leading position,
I would not trust you to do an operation.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Ben Bacarisse

9/26/2015 9:59:00 AM

0

jonas.thornvall@gmail.com writes:

> Den lördag 26 september 2015 kl. 00:11:31 UTC+2 skrev Danny:
>> to jonas.t
>>
>> in short, if the value is an integer, it will never return NaN.
>> What if may be happening in your case, is that the object gets
>> something else other than a numeric value.
>>
>> but a solution depends on what the context for all that is
>> and you haven't given us context or relevant code of the matter
>
> Well sometimes the index of the array is empty for the leading
> position, it is an carry operation.

As yet unassigned positions in an array are undefined but the isNaN
function returned true for undefined values. It's likely that you don't
have a numeric NaN in addOne[i], but undefined instead.

--
Ben.

Evertjan.

9/26/2015 11:15:00 AM

0

Ben Bacarisse <ben.usenet@bsb.me.uk> wrote on 26 Sep 2015 in
comp.lang.javascript:

>> Well sometimes the index of the array is empty for the leading
>> position, it is an carry operation.
>
> As yet unassigned positions in an array are undefined but the isNaN
> function returned true for undefined values. It's likely that you don't
> have a numeric NaN in addOne[i], but undefined instead.

You are mixing things up.

The index[!] of an array can never be "empty",
only the array item[!] can be null or unassignesd.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

JT

9/26/2015 11:25:00 AM

0

Den lördag 26 september 2015 kl. 08:59:50 UTC+2 skrev Evertjan.:
> jonas.thornvall@gmail.com wrote on 26 Sep 2015 in comp.lang.javascript:
>
> > Well sometimes the index of the array is empty for the leading position,
> > it is an carry operation.
>
> An index of an array CANNOT be empty,
> because then it would not be an index.

Ok ok, Evertjan the element of index is NaN.

> Till you are in a leading position,
> I would not trust you to do an operation.

Well that is the problem with doing carry operations using array elements as digitplaces, you do not know if the element for the index previously occupied by an integer or not. And if not you have to initialise it to an integer. The main case is of course you add one to the array element. But if it is a leading carry operation the element for the index will hold NaN.

> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)

JT

9/26/2015 11:28:00 AM

0

Den lördag 26 september 2015 kl. 11:59:23 UTC+2 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
>
> > Den lördag 26 september 2015 kl. 00:11:31 UTC+2 skrev Danny:
> >> to jonas.t
> >>
> >> in short, if the value is an integer, it will never return NaN.
> >> What if may be happening in your case, is that the object gets
> >> something else other than a numeric value.
> >>
> >> but a solution depends on what the context for all that is
> >> and you haven't given us context or relevant code of the matter
> >
> > Well sometimes the index of the array is empty for the leading
> > position, it is an carry operation.
>
> As yet unassigned positions in an array are undefined but the isNaN
> function returned true for undefined values. It's likely that you don't
> have a numeric NaN in addOne[i], but undefined instead.
>
> --
> Ben.

Yes that is the problem Ben when you use array elements as digits places. If it is an add operation that will carry extra digit you will add to an index where the element may be a NaN value.