Den lördag 16 maj 2015 kl. 11:58:18 UTC+2 skrev jonas.t...@gmail.com:
> Den lördag 16 maj 2015 kl. 11:37:44 UTC+2 skrev Evertjan.:
> > jonas.thornvall@gmail.com wrote on 16 mei 2015 in comp.lang.javascript:
> >
> > > I have arrays that are read from right to left, to do arithmetic upon.
> >
> > arrays do not have right or left.
> > You again are making up your own terminology.
> >
> > > But at some point i of course want a printout of the elements of the
> > > array.
> >
> > Why "of course"?
> >
> > If you want to print out the inverse of an array:
> >
> > var arrlen = arr.length;
> > for (var i=arrlen;;i--) document.write(arr[i-1]+'<br>');
> >
> >
> > > So i of course reverse the array,
> >
> > Why "of course"?
> >
> > Reversing an array is a timeconsuming and needless action,
> > as you can easily use a short formula for an inversed index.
> >
> > arr[arrlen-1-i]
> >
> > > *but* the integer/string elements
> >
> > arrays heve elements,
> > the type of content of which do not toutch a reversing.
> >
> > > of course remain reversed.
> >
> > Why "of course"?
> > So you want to change them back, and again consume exponential time?
> >
> > > Is there a preferable way to do this,
> >
> > Yes!
> >
> > Don't reverse physically, manipulate the index.
> >
> > > i do understand that i can make a
> > > function that reverse
> >
> > Well, if you understand that you can do something, it must be okay.
> > I for one don't understand what you mean.
> >
> > > the integer/string digit by digit.
> >
> > You must mean "element by element", arrays don't have digits.
> > Strings too don't have digits, they have characters.
> >
> > You again are making up your own terminology.
> > That is accceptable if you don't pester others with it,
> > but you eventually fall into your own trap,
> > when you will misunderstand the publications of others.
> >
> > ================================
> >
> > The preferable way is not to inverse long arrays,
> > as, see above and my earlier mail,
> > using an index-manipulation-formula has all the necessary ingredients.
> >
> > Mind that "preferable" depends on what your goals are, if you want
> > unreadable, messy, exponential timeconsuming, buggy and difficult to debug
> > code "of course" your approach might be different.
>
> (No i did not mean element by element, i meant what i said. Reverse each element digit by digit)
>
> An example Janevert
>
> 21 should encode to [1,10] in base eleven because 11+10.
> But is stored in array as [01,1]
>
> Now i want a printout 1,10
>
> And you say i should not physically alter the array at output, and i do not see how it is even possible without touching the array. You could read it into a commaseparated string and reverse that but i doubt that is what you have in mind for efficiency.
This is what i can come up with, is it correct and fast enough?
var myArr=[01,1];
var printStr="";
reverse_array(myArr);
function reverse_array(thisArr){
var arrLength;
arrLength=thisArr.length;
for (i=arrLength;i>=0;i--){
thisElement=thisArr[i];
reverse_element(thisElement);
}
}
function reverse_element(element) {
index=element.length;
while(index > 0) {
index=index-1;
printStr=+element.toString()[index];
}
}