[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.javascript

Reach a digit of numerical value in array index.

JT

3/17/2015 9:16:00 PM

var Compute=[] is an array/stack holding integer values now i want to add individual digits store them in result.

i=0;
result[i]=compute[0][i]+compute[1][i]; //Well this does not work

Is it even possible without saving the index?
Is this really necessary?
i=0;
intA=compute[0];
intB=compute[1]
result[i]=intA+intB;
19 Answers

JT

3/17/2015 9:35:00 PM

0

Den tisdag 17 mars 2015 kl. 22:16:04 UTC+1 skrev jonas.t...@gmail.com:
> var Compute=[] is an array/stack holding integer values now i want to add individual digits store them in result.
>
> i=0;
> result[i]=compute[0][i]+compute[1][i]; //Well this does not work
>
> Is it even possible without saving the index?
> Is this really necessary?
> i=0;
> intA=compute[0];
> intB=compute[1]
> result[i]=intA+intB;

Ok i am tired, there is no way to reach the index witin an integer in javascript?
It seem it was very stupid of me converting the strings to numbers in the first place.

But really?

JJ

3/18/2015 6:38:00 AM

0

On Tue, 17 Mar 2015 14:34:56 -0700 (PDT), jonas.thornvall@gmail.com wrote:
> Den tisdag 17 mars 2015 kl. 22:16:04 UTC+1 skrev jonas.t...@gmail.com:
>> var Compute=[] is an array/stack holding integer values now i want to add individual digits store them in result.
>>
>> i=0;
>> result[i]=compute[0][i]+compute[1][i]; //Well this does not work

From what I can see, it doesn't work because "result" is not yet defined.

>> Is it even possible without saving the index?
>> Is this really necessary?
>> i=0;
>> intA=compute[0];
>> intB=compute[1]
>> result[i]=intA+intB;

Yes: result[0]=intA+intB;

> Ok i am tired, there is no way to reach the index witin an integer in javascript?

If you meant to get the index of a specific value of an array, use this:

var valueToSearch = 3;
var valueIndex = myArray.indexOf(valueToSearch);

> It seem it was very stupid of me converting the strings to numbers in the first place.
>
> But really?

What string?

JT

3/18/2015 1:57:00 PM

0

Den onsdag 18 mars 2015 kl. 07:38:29 UTC+1 skrev JJ:
> On Tue, 17 Mar 2015 14:34:56 -0700 (PDT), jonas.thornvall@gmail.com wrote:
> > Den tisdag 17 mars 2015 kl. 22:16:04 UTC+1 skrev jonas.t...@gmail.com:
> >> var Compute=[] is an array/stack holding integer values now i want to add individual digits store them in result.
> >>
> >> i=0;
> >> result[i]=compute[0][i]+compute[1][i]; //Well this does not work
>
> From what I can see, it doesn't work because "result" is not yet defined.

No it really does not work, compute is an array of integers, and you can not treat it as a two dimensional array to reach a digit.


> >> Is it even possible without saving the index?
> >> Is this really necessary?
> >> i=0;
> >> intA=compute[0];
> >> intB=compute[1]
> >> result[i]=intA+intB;
>
> Yes: result[0]=intA+intB;
>
> > Ok i am tired, there is no way to reach the index witin an integer in javascript?
>
> If you meant to get the index of a specific value of an array, use this:
>
> var valueToSearch = 3;
> var valueIndex = myArray.indexOf(valueToSearch);

I think that the result not an integer from indexOf, and than it does not make sense to use parseInt again.

> > It seem it was very stupid of me converting the strings to numbers in the first place.
> >
> > But really?
>
> What string?

Norman Peelman

3/19/2015 3:50:00 AM

0

On 03/17/2015 05:15 PM, jonas.thornvall@gmail.com wrote:
> var Compute=[] is an array/stack holding integer values now i want to add individual digits store them in result.
>
> i=0;
> result[i]=compute[0][i]+compute[1][i]; //Well this does not work
>
> Is it even possible without saving the index?
> Is this really necessary?
> i=0;
> intA=compute[0];
> intB=compute[1]
> result[i]=intA+intB;
>

You aren't dealing with strings here, it doesn't work that way.


--
Norman
Registered Linux user #461062
AMD64X2 6400+ Ubuntu 10.04 64bit

JT

3/19/2015 8:17:00 AM

0

Den torsdag 19 mars 2015 kl. 04:49:49 UTC+1 skrev Norman Peelman:
> On 03/17/2015 05:15 PM, jonas.thornvall@gmail.com wrote:
> > var Compute=[] is an array/stack holding integer values now i want to add individual digits store them in result.
> >
> > i=0;
> > result[i]=compute[0][i]+compute[1][i]; //Well this does not work
> >
> > Is it even possible without saving the index?
> > Is this really necessary?
> > i=0;
> > intA=compute[0];
> > intB=compute[1]
> > result[i]=intA+intB;
> >
>
> You aren't dealing with strings here, it doesn't work that way.
>
>
> --
> Norman
> Registered Linux user #461062
> AMD64X2 6400+ Ubuntu 10.04 64bit

Well i know howto do it, but its insane, i questioning why one can not read/reach the digit place of a number stored in an array, without going via temp variables. There should be a way reading them straight out of the index.

John Harris

3/19/2015 9:28:00 AM

0

On Thu, 19 Mar 2015 01:16:40 -0700 (PDT), jonas.thornvall@gmail.com
wrote:


<snip>
>Well i know howto do it, but its insane,
>i questioning why one can not read/reach the digit place
>of a number stored in an array, without going via temp variables.
>There should be a way reading them straight out of the index.

Why should there be?

Remember that what's stored are binary numbers. Do you really want to
fetch a binary digit?

John

JT

3/19/2015 12:01:00 PM

0

Den torsdag 19 mars 2015 kl. 10:28:12 UTC+1 skrev John Harris:
> On Thu, 19 Mar 2015 01:16:40 -0700 (PDT), jonas.thornvall@gmail.com
> wrote:
>
>
> <snip>
> >Well i know howto do it, but its insane,
> >i questioning why one can not read/reach the digit place
> >of a number stored in an array, without going via temp variables.
> >There should be a way reading them straight out of the index.
>
> Why should there be?
>
> Remember that what's stored are binary numbers. Do you really want to
> fetch a binary digit?
>
> John

Yeah why not i guess that make the adder faster, doing bit addition rather than byte. Well it may end up being the same work but that is not the point.

Thomas 'PointedEars' Lahn

3/19/2015 2:59:00 PM

0

John Harris wrote:

> [â?¦] jonas.thornvall@gmail.com wrote:
>> Well i know howto do it, but its insane,
>> i questioning why one can not read/reach the digit place
>> of a number stored in an array, without going via temp variables.
>> There should be a way reading them straight out of the index.

> Why should there be?
>
> Remember that what's stored are binary numbers. Do you really want to
> fetch a binary digit?

ACK. That said, there are (at least) three ways to retrieve the decimal
digit from a Number value without a "temporary variable"; just error-prone
and not very efficient ones (so kids, donâ??t do this at home):

a) n.toString().charAt(i)
b) n.toString().slice(-j, -j + 1)
c) (n % Math.pow(10, j)).toString().charAt(0)

where â??nâ? is (a variable storing) the Number value (preferably a numeric
integer), â??iâ? is (a variable storing) the 0-based index of the digit counted
from the left, and â??jâ? is (a variable storing) the 1-based index of the
digit counted from the right.

I do not think I need to explain why these approaches are not very
efficient. They are also error-prone because of limited precision of the
IEEE 754 double-precision floating-point format as such, and of the string
representation of its values in ECMAScript.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Thomas 'PointedEars' Lahn

3/19/2015 3:13:00 PM

0

Thomas 'PointedEars' Lahn wrote:

> [â?¦] there are (at least) three ways to retrieve the decimal
> digit from a Number value without a "temporary variable"; just error-prone
> and not very efficient ones (so kids, donâ??t do this at home):
>
> a) n.toString().charAt(i)
> b) n.toString().slice(-j, -j + 1)
> c) (n % Math.pow(10, j)).toString().charAt(0)
>
> where â??nâ? is (a variable storing) the Number value (preferably a numeric
> integer), â??iâ? is (a variable storing) the 0-based index of the digit
> counted from the left, and â??jâ? is (a variable storing) the 1-based index
> of the digit counted from the right.
>
> I do not think I need to explain why these approaches are not very
> efficient. They are also error-prone because of limited precision of the
> IEEE 754 double-precision floating-point format as such, and of the string
> representation of its values in ECMAScript.

For recreation, here is another one:

d) Math.floor((n % Math.pow(10, j)) / Math.pow(10, j - 1))

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

John Harris

3/19/2015 4:23:00 PM

0

On Thu, 19 Mar 2015 05:00:36 -0700 (PDT), jonas.thornvall@gmail.com
wrote:

>Den torsdag 19 mars 2015 kl. 10:28:12 UTC+1 skrev John Harris:
>> On Thu, 19 Mar 2015 01:16:40 -0700 (PDT), jonas.thornvall@gmail.com
>> wrote:
>>
>>
>> <snip>
>> >Well i know howto do it, but its insane,
>> >i questioning why one can not read/reach the digit place
>> >of a number stored in an array, without going via temp variables.
>> >There should be a way reading them straight out of the index.
>>
>> Why should there be?
>>
>> Remember that what's stored are binary numbers. Do you really want to
>> fetch a binary digit?
>>
>> John
>
>Yeah why not i guess that make the adder faster, doing bit addition rather than byte. Well it may end up being the same work but that is not the point.

On second thoughts I haven't the faintest idea what you are saying and
what your question is.

Are you talking about an array of ECMAScript numbers, an array of
strings holding a representation of a big number, or an array of
arrays of ECMAScript numbers holding a representation of a big number?


John