[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.javascript

Reverse elements in array, they can be integers or strings.

JT

5/16/2015 9:03:00 AM

I have arrays that are read from right to left, to do arithmetic upon.

But at some point i of course want a printout of the elements of the array.
So i of course reverse the array, *but* the integer/string elements of course remain reversed.

Is there a preferable way to do this, i do understand that i can make a function that reverse the integer/string digit by digit.

49 Answers

JT

5/16/2015 9:10:00 AM

0

Den lördag 16 maj 2015 kl. 11:03:30 UTC+2 skrev jonas.t...@gmail.com:
> I have arrays that are read from right to left, to do arithmetic upon.
>
> But at some point i of course want a printout of the elements of the array.
> So i of course reverse the array, *but* the integer/string elements of course remain reversed.
>
> Is there a preferable way to do this, i do understand that i can make a function that reverse the integer/string digit by digit.

A connecting question, maybe..

When i store an integer as an array element it take up 32 bits?

But are the characters of a string UTF-8 thus an array element string of 4 characters 32 bits?

Evertjan.

5/16/2015 9:38:00 AM

0

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.

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

JT

5/16/2015 9:58:00 AM

0

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.

Evertjan.

5/16/2015 10:03:00 AM

0

jonas.thornvall@gmail.com wrote on 16 mei 2015 in comp.lang.javascript:

Please do not change the subject without starting a new thread.

> When i store an integer as an array element it take up 32 bits?

That is not a question you should ask us.

array elements store variable values,
that can be strings, arrays, objecs of floating-point numbers.

Integer is not a variable type in Javascript.

If you decide to store "integer values" [not "integers"] as Javascript
floating-point numbers, even then the internal "it takes up 32 bits?"
[grammar corrected] is unimportant to you and even might be different for
different Javascript implementations. The only thing you need to know is the
maximum positive and negative integer values allowed:

<script type='text/javascript'>

var N = Number.MAX_SAFE_INTEGER;

document.write(N); // 9007199254740991
document.write('<br>');

document.write(N + 1); // 9007199254740992
document.write('<br>');

document.write(N + 2); // 9007199254740992
document.write('<br>');

document.write( N === Math.pow(2, 53)-1 ); // true
document.write('<br>');
document.write('<br>');

N = Number.MIN_SAFE_INTEGER;

document.write(N); // -9007199254740991

document.write('<br>');

document.write(N - 1); // -9007199254740992
document.write('<br>');

document.write(N - 2); // -9007199254740992
document.write('<br>');

</script>

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

Evertjan.

5/16/2015 10:23:00 AM

0

jonas.thornvall@gmail.com wrote on 16 mei 2015 in comp.lang.javascript:
> An example Janevert

If you cannot even write my name consistently,
how the hell do you expect to write consistent code?

> 21 should encode to [1,10] in base eleven because 11+10.
> But is stored in array as [01,1]

If it is stored in an array element, then that array element is an array.

> 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

I just showed you.

If you can invert an array in fact what you are doing is taking the elements
of an array according to a index manipulation function [meaning not just
from zero to arrlen-1], and then storing each element in another place [in
an other array, or maybe swapping elements in the same array]

If instead of the whole storing rigmarole, you immediately print that
element, why would that not be possible?

And when a function internally 'needs' an inverted array, the same is true
that the function can use the same index manipulation and so forgo the need
of an physically inverted array too.

When printing an array, you will have to fetch each element anyway.

> without touching the array

Again you make up your own newspeak, what the hell is "touching the array"?

> You could read it into a commaseparated string and reverse that

No, I would not,
we were talking about inversing an array before printing it.

> but i doubt that is what you have in mind for efficiency.

DO NOT INVERT VERY LARGE ARRAYS,
AS there is no NEED for that,
because it only will cost you time.

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

JT

5/16/2015 10:38:00 AM

0

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

JT

5/16/2015 10:44:00 AM

0

Den lördag 16 maj 2015 kl. 12:22:45 UTC+2 skrev Evertjan.:
> jonas.thornvall@gmail.com wrote on 16 mei 2015 in comp.lang.javascript:
> > An example Janevert
>
> If you cannot even write my name consistently,
> how the hell do you expect to write consistent code?
>
> > 21 should encode to [1,10] in base eleven because 11+10.
> > But is stored in array as [01,1]
>
> If it is stored in an array element, then that array element is an array.
>
> > 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
>
> I just showed you.
>
> If you can invert an array in fact what you are doing is taking the elements
> of an array according to a index manipulation function [meaning not just
> from zero to arrlen-1], and then storing each element in another place [in
> an other array, or maybe swapping elements in the same array]
>
> If instead of the whole storing rigmarole, you immediately print that
> element, why would that not be possible?
>
> And when a function internally 'needs' an inverted array, the same is true
> that the function can use the same index manipulation and so forgo the need
> of an physically inverted array too.
>
> When printing an array, you will have to fetch each element anyway.
>
> > without touching the array
>
> Again you make up your own newspeak, what the hell is "touching the array"?
>
> > You could read it into a commaseparated string and reverse that
>
> No, I would not,
> we were talking about inversing an array before printing it.
>
> > but i doubt that is what you have in mind for efficiency.
>
> DO NOT INVERT VERY LARGE ARRAYS,
> AS there is no NEED for that,
> because it only will cost you time.
>
> --


I have to be critical Evertjan, what you show do not reverse the digits/characters of the the element, only the order of the element.

There is a difference you know.

var arrlen = arr.length;
for (var i=arrlen;;i--) document.write(arr[i-1]+'<br>');

This code is useless and that is a fact.

Ben Bacarisse

5/16/2015 11:02:00 AM

0

jonas.thornvall@gmail.com writes:
<snip>
> 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

You are still confusing numbers and their representations (or, maybe,
you now understand but have not found a way to write about the
distinction). Either way, it makes it impossible to communicate about
numbers and their representations. There is a simple solution:
console.log prints an unambiguous representation of a value. You could
use that even if you don't fully understand the distinction it's making.
For example, this method will distinguish between a string "[01,1]", an
array of numbers [1,1], and an array of strings ["01","1"].

You seem to be asking how to turn [1,10] into "1,10" but that's too easy
to be the question.

<snip>
--
Ben.

JT

5/16/2015 11:15:00 AM

0

Den lördag 16 maj 2015 kl. 13:01:50 UTC+2 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
> <snip>
> > 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
>
> You are still confusing numbers and their representations (or, maybe,
> you now understand but have not found a way to write about the
> distinction). Either way, it makes it impossible to communicate about
> numbers and their representations. There is a simple solution:
> console.log prints an unambiguous representation of a value. You could
> use that even if you don't fully understand the distinction it's making.
> For example, this method will distinguish between a string "[01,1]", an
> array of numbers [1,1], and an array of strings ["01","1"].
>
> You seem to be asking how to turn [1,10] into "1,10" but that's too easy
> to be the question.
>
> <snip>
> --
> Ben.

No thar is not the question, i want to reverse the array and reverse the digits of *each element* within array on *decimal level*.

I am doing arithmetic upon the elements so i know they are not stored as characters and strings.

So i *assume* that the element stored at each index within array is an integer.

Really how hard can it be to understand.

Ben Bacarisse

5/16/2015 12:11:00 PM

0

jonas.thornvall@gmail.com writes:

> Den lördag 16 maj 2015 kl. 13:01:50 UTC+2 skrev Ben Bacarisse:
>> jonas.thornvall@gmail.com writes:
>> <snip>
>> > 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
>>
>> You are still confusing numbers and their representations (or, maybe,
>> you now understand but have not found a way to write about the
>> distinction). Either way, it makes it impossible to communicate about
>> numbers and their representations. There is a simple solution:
>> console.log prints an unambiguous representation of a value. You could
>> use that even if you don't fully understand the distinction it's making.
>> For example, this method will distinguish between a string "[01,1]", an
>> array of numbers [1,1], and an array of strings ["01","1"].
>>
>> You seem to be asking how to turn [1,10] into "1,10" but that's too easy
>> to be the question.
<snip>

> No thar is not the question, i want to reverse the array and reverse
> the digits of *each element* within array on *decimal level*.

At some point you will start to take people's advice. Until then I
don't think you will make much headway.

Show the output from console.log of the array you are starting with.
Then show the output from console.log of the "thing" you want. I don't
even know if you want a string or an array, and if you want an array, I
don't know what type of elements it should have. The output from
console.log will make it clear to other what task you are trying to
perform.

> I am doing arithmetic upon the elements so i know they are not stored
> as characters and strings.

OK. (I note, however, that in one bit of code you once posted you
pointlessly converted them to strings only to immediately re-convert
them back to numbers. This made me think that, at the time, you did not
know which you had in the array.)

> So i *assume* that the element stored at each index within array is an
> integer.

Since this is your program, you can know what it is. There should be no
need to assume it. This makes you statement "stored in array as [01,1]"
very odd. That looks like an array of two integers, both of which are 1.

> Really how hard can it be to understand.

And how hard is it to do what I ask? Show the console.log output for
the array you have and then do the same for the string you want to
print.

--
Ben.