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