Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #27714

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

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Reverse elements in array, they can be integers or strings.
Date 2015-05-17 10:37 +0000
Organization A noiseless patient Spider
Message-ID <mj9r1a$ods$1@dont-email.me> (permalink)
References (2 earlier) <fdb99a69-c853-4c3a-baca-b2de2d605a12@googlegroups.com> <fafb0168-4124-415e-9f02-7775f0256619@googlegroups.com> <XnsA49C98B56DF71eejj99@194.109.133.246> <mj8ik2$f1c$4@dont-email.me> <87lhgom7ah.fsf@bsb.me.uk>

Show all headers | View raw


On Sun, 17 May 2015 00:28:22 +0100, Ben Bacarisse wrote:

> Denis McMahon <denismfmcmahon@gmail.com> writes:
> 
>> On Sat, 16 May 2015 15:00:42 +0200, Evertjan. wrote:
>>
>>> jonas.thornvall@gmail.com wrote on 16 mei 2015 in
>>> comp.lang.javascript:
>>
>>>> var arrLength; arrLength=thisArr.length; for (i=arrLength;i>=0;i--){
>>>>      thisElement=thisArr[i];
>>
>>> this will give an ERROR,
>>> as the highest indexed element has an index of i-1
>>
>> Will it?
> 
> The code is wrong, but whether it "gives and ERROR" depends on what was
> meant by that.
> 
>> Given i starting at thisArr.length and executing an i-- before
>> accessing the array, I don't think it will give that error, the error
>> will be on the pass after i == 0, as it will then decrement i and
>> execute the loop with i having a value of -1, which is an illegal array
>> index.
> 
> You've got the place where the decrement happens mixed up -- it is done
> after each execution of the loop body, so the loop above initially
> accesses an undefined element, and then accesses all of the defined
> elements in sequence.  An index of -1 is not used (though i == -1 at the
> end of the loop's execution).

HeadDesk! Doh, yeah, apologies to Evertjan.

>> All of the following are valid in c-type languages where 0 is "falsey",
>> numbers other than 0 are "truthy" and arrays are 0 indexed:
>>
>> for (var i = arr.length; i > 0; i--) {
>> // do something with arr[i]
>> }
>>
>> for (var i = arr.length; i; i--) {
>> // do something with arr[i]
>> }
> 
> No, these are both wrong.  They access an undefined element first, and
> neither accesses element 0.
> 
>> var i = arr.length;
>> while (i--) {
>> // do something with arr[i]
>> }
> 
> This one is correct.
> 
> <snip>

I shall shut up now.

-- 
Denis McMahon, denismfmcmahon@gmail.com

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-16 02:03 -0700
  Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-16 02:10 -0700
    MAX_SAFE_INTEGER & MIN_SAFE_INTEGER, was Re: Reverse elements in array, they can be integers or strings. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-16 12:03 +0200
  Re: Reverse elements in array, they can be integers or strings. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-16 11:37 +0200
    Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-16 02:58 -0700
      Re: Reverse elements in array, they can be integers or strings. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-16 12:22 +0200
        Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-16 03:44 -0700
          Re: Reverse elements in array, they can be integers or strings. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-16 14:52 +0200
            Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-16 06:09 -0700
              Re: Reverse elements in array, they can be integers or strings. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-16 15:30 +0200
      Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-16 03:37 -0700
        Re: Reverse elements in array, they can be integers or strings. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-16 15:00 +0200
          Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-16 06:24 -0700
            Re: Reverse elements in array, they can be integers or strings. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-16 15:54 +0200
              Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-16 07:01 -0700
                Re: Reverse elements in array, they can be integers or strings. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-16 21:05 +0200
          Re: Reverse elements in array, they can be integers or strings. Denis McMahon <denismfmcmahon@gmail.com> - 2015-05-16 23:07 +0000
            Re: Reverse elements in array, they can be integers or strings. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-17 00:28 +0100
              Re: Reverse elements in array, they can be integers or strings. Denis McMahon <denismfmcmahon@gmail.com> - 2015-05-17 10:37 +0000
              Re: Reverse elements in array, they can be integers or strings. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-17 13:34 +0200
      Re: Reverse elements in array, they can be integers or strings. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-16 12:01 +0100
        Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-16 04:14 -0700
          Re: Reverse elements in array, they can be integers or strings. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-16 13:11 +0100
            Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-16 06:37 -0700
              Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-16 06:54 -0700
                Re: Reverse elements in array, they can be integers or strings. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-16 15:58 +0200
                Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-16 07:04 -0700
                Re: Reverse elements in array, they can be integers or strings. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-16 21:16 +0200
                Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-16 07:51 -0700
                Re: Reverse elements in array, they can be integers or strings. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-16 21:25 +0200
              Re: Reverse elements in array, they can be integers or strings. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-16 21:41 +0100
                Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-16 16:51 -0700
                Re: Reverse elements in array, they can be integers or strings. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-17 11:14 +0200
                Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-17 03:04 -0700
                Re: Reverse elements in array, they can be integers or strings. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-17 12:40 +0200
                Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-17 06:04 -0700
                Re: Reverse elements in array, they can be integers or strings. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-17 21:47 +0200
                Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-17 15:06 -0700
                Re: Reverse elements in array, they can be integers or strings. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-18 00:57 +0200
                Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-17 17:10 -0700
                Re: Reverse elements in array, they can be integers or strings. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-18 13:16 +0200
                Re: Reverse elements in array, they can be integers or strings. John Harris <niam@jghnorth.org.uk.invalid> - 2015-05-18 10:28 +0100
                Re: Reverse elements in array, they can be integers or strings. Denis McMahon <denismfmcmahon@gmail.com> - 2015-05-18 16:44 +0000
                Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-18 10:16 -0700
                Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-18 10:13 -0700
                Re: Reverse elements in array, they can be integers or strings. John Harris <niam@jghnorth.org.uk.invalid> - 2015-05-18 19:42 +0100
                Re: Reverse elements in array, they can be integers or strings. jonas.thornvall@gmail.com - 2015-05-17 02:58 -0700
            Re: Reverse elements in array, they can be integers or strings. Denis McMahon <denismfmcmahon@gmail.com> - 2015-05-16 22:41 +0000
          Re: Reverse elements in array, they can be integers or strings. "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-16 15:11 +0200
      Re: Reverse elements in array, they can be integers or strings. John Harris <niam@jghnorth.org.uk.invalid> - 2015-05-17 11:27 +0100

csiph-web