[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.javascript

Merge indexes..

JT

5/20/2015 6:59:00 AM

I need to merge integers at indexes within an array into an integer?
[7,9,8] = 798

No join i need an integer value.

How?


18 Answers

JT

5/20/2015 8:21:00 AM

0

Den onsdag 20 maj 2015 kl. 08:59:15 UTC+2 skrev jonas.t...@gmail.com:
> I need to merge integers at indexes within an array into an integer?
> [7,9,8] = 798
>
> No join i need an integer value.
>
> How?

Sorry i was thinking wrong should be 897, i have one way todo it. But i was thinking it should be possible to do with just concatenations rather than arithmetic?

<script>
function merge(){
for (i=0;i<myArr.length;i++){
myInt=(myArr[i]*x)+myInt;
x=x*10;
}
}
myArr =[7,9,8]
myInt=0;
x=1;
merge();
document.write(myInt);
</script>

Ben Bacarisse

5/20/2015 10:43:00 AM

0

jonas.thornvall@gmail.com writes:

> Den onsdag 20 maj 2015 kl. 08:59:15 UTC+2 skrev jonas.t...@gmail.com:
>> I need to merge integers at indexes within an array into an integer?
>> [7,9,8] = 798
>>
>> No join i need an integer value.
>>
>> How?
>
> Sorry i was thinking wrong should be 897, i have one way todo it. But
> i was thinking it should be possible to do with just concatenations
> rather than arithmetic?

That's because you are still not clear about the distinction between a
number and its representation. If you have Number values (that's an
ECMAScrip type) then arithmetic is the correct way to produce new
numbers like this.

> <script>
> function merge(){
> for (i=0;i<myArr.length;i++){
> myInt=(myArr[i]*x)+myInt;
> x=x*10;
> }
> }
> myArr =[7,9,8]
> myInt=0;
> x=1;
> merge();
> document.write(myInt);
> </script>

Don't make you code worse by adding yet more function that communicate
via globals. This operation is a self-contained function of the array.
Pass the array as a parameter and return the result from the function.

--
Ben.

Andrew Poulos

5/20/2015 10:55:00 AM

0

On 20/05/2015 4:59 PM, jonas.thornvall@gmail.com wrote:
> I need to merge integers at indexes within an array into an integer?
> [7,9,8] = 798
>
> No join i need an integer value.

What about

var a = [7, 9, 8],
m = parseInt( a.join(''), 10);

?

Andrew Poulos

Michael Haufe (\"TNO\")

5/20/2015 11:51:00 AM

0

On Wednesday, May 20, 2015 at 5:55:13 AM UTC-5, Andrew Poulos wrote:
> What about
>
> var a = [7, 9, 8],
> m = parseInt( a.join(''), 10);

m = Number(a.join(''));

Thomas 'PointedEars' Lahn

5/20/2015 12:32:00 PM

0

Michael Haufe (TNO) wrote:

> On Wednesday, May 20, 2015 at 5:55:13 AM UTC-5, Andrew Poulos wrote:
>> What about
>>
>> var a = [7, 9, 8],
>> m = parseInt( a.join(''), 10);
>
> m = Number(a.join(''));

[0, 1, 2]

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

Christoph M. Becker

5/20/2015 1:12:00 PM

0

Thomas 'PointedEars' Lahn wrote:

> Michael Haufe (TNO) wrote:
>
>> On Wednesday, May 20, 2015 at 5:55:13 AM UTC-5, Andrew Poulos wrote:
>>> What about
>>>
>>> var a = [7, 9, 8],
>>> m = parseInt( a.join(''), 10);
>>
>> m = Number(a.join(''));
>
> [0, 1, 2]

According to ES 5.1, section 9.3[1]:

| A StringNumericLiteral that is decimal may have any number of leading
| 0 digits.

[1] <http://ecma-international.org/ecma-262/5.1/#s...

--
Christoph M. Becker

JT

5/20/2015 1:16:00 PM

0

Den onsdag 20 maj 2015 kl. 12:43:00 UTC+2 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
>
> > Den onsdag 20 maj 2015 kl. 08:59:15 UTC+2 skrev jonas.t...@gmail.com:
> >> I need to merge integers at indexes within an array into an integer?
> >> [7,9,8] = 798
> >>
> >> No join i need an integer value.
> >>
> >> How?
> >
> > Sorry i was thinking wrong should be 897, i have one way todo it. But
> > i was thinking it should be possible to do with just concatenations
> > rather than arithmetic?
>
> That's because you are still not clear about the distinction between a
> number and its representation. If you have Number values (that's an
> ECMAScrip type) then arithmetic is the correct way to produce new
> numbers like this.
>
> > <script>
> > function merge(){
> > for (i=0;i<myArr.length;i++){
> > myInt=(myArr[i]*x)+myInt;
> > x=x*10;
> > }
> > }
> > myArr =[7,9,8]
> > myInt=0;
> > x=1;
> > merge();
> > document.write(myInt);
> > </script>
>
> Don't make you code worse by adding yet more function that communicate
> via globals. This operation is a self-contained function of the array.
> Pass the array as a parameter and return the result from the function.
>
> --
> Ben.

It was actually just a draw here is the one in script.
"I got it to work see next post, do both arithmetic on unsigned pairs, *arithmetic and conversion in anybase* see new post.

Now i can change the digit add within conversion to either a division or a search, have not decided which. It should make a big difference converting very big numbers.

/* MERGE THE INTEGER INDEXES OF AN ARRAY INTO ONE INTEGER */

function mergeArr(myArr)
{
myInt = 0;
x = 1;
for (i = 0; i < myArr.length; i ++ )
{
myInt = (myArr[i] * x) + myInt;
x = x * 10;
}
return myInt;
}

John Harris

5/20/2015 1:21:00 PM

0

On Wed, 20 May 2015 11:42:55 +0100, Ben Bacarisse
<ben.usenet@bsb.me.uk> wrote:

>jonas.thornvall@gmail.com writes:
>

<snip>
>> <script>
>> function merge(){
>> for (i=0;i<myArr.length;i++){
>> myInt=(myArr[i]*x)+myInt;
>> x=x*10;
>> }
>> }
>> myArr =[7,9,8]
>> myInt=0;
>> x=1;
>> merge();
>> document.write(myInt);
>> </script>
>
>Don't make you code worse by adding yet more function that communicate
>via globals. This operation is a self-contained function of the array.
>Pass the array as a parameter and return the result from the function.

Even better, pass the base, x, as a parameter as well.

John

Thomas 'PointedEars' Lahn

5/20/2015 1:33:00 PM

0

John Harris wrote:

> [â?¦] Ben Bacarisse [â?¦] wrote:
>> jonas.thornvall@gmail.com writes:
>>> <script>
>>> function merge(){
>>> for (i=0;i<myArr.length;i++){
>>> myInt=(myArr[i]*x)+myInt;
>>> x=x*10;
>>> }
>>> }
>>> myArr =[7,9,8]
>>> myInt=0;
>>> x=1;
>>> merge();
>>> document.write(myInt);
>>> </script>
>>
>>Don't make you code worse by adding yet more function that communicate
>>via globals. This operation is a self-contained function of the array.
>>Pass the array as a parameter and return the result from the function.
>
> Even better, pass the base, x, as a parameter as well.

And for bases from 2 to 36, just return parseInt(a.join(""), base), with
both â??aâ? and â??baseâ? being formal parameters of merge(). No need to reinvent
Wheel 0.1:

<http://ecma-international.org/ecma-262/5.1/#sec-15...

Finally, make merge() the valueOf() method of your own object type, so that
â??aâ? and maybe even â??baseâ? can be replaced by references to properties of the
instance.

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

5/20/2015 1:52:00 PM

0

Thomas 'PointedEars' Lahn wrote:

> John Harris wrote:
>> [â?¦] Ben Bacarisse [â?¦] wrote:
>>> jonas.thornvall@gmail.com writes:
>>>> <script>
>>>> function merge(){
>>>> for (i=0;i<myArr.length;i++){
>>>> myInt=(myArr[i]*x)+myInt;
>>>> x=x*10;
>>>> }
>>>> }
>>>> myArr =[7,9,8]
>>>> myInt=0;
>>>> x=1;
>>>> merge();
>>>> document.write(myInt);
>>>> </script>
>>>
>>>Don't make you code worse by adding yet more function that communicate
>>>via globals. This operation is a self-contained function of the array.
>>>Pass the array as a parameter and return the result from the function.
>>
>> Even better, pass the base, x, as a parameter as well.
>
> And for bases from 2 to 36, just return parseInt(a.join(""), base), with
> both â??aâ? and â??baseâ? being formal parameters of merge(). No need to
> reinvent Wheel 0.1:
>
> <http://ecma-international.org/ecma-262/5.1/#sec-15...

This optimization works only if the elements of the array are the digits in
the other base (e.g., strings of length 1 if necessary), not the decimal
digit-values. So it definitely works only for bases 2 to 10 where the
string representation of each digit always has length 1 (no matter whether
it is stored as a string or a number).

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