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


Groups > comp.lang.javascript > #27702

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-16 23:07 +0000
Organization A noiseless patient Spider
Message-ID <mj8ik2$f1c$4@dont-email.me> (permalink)
References <2ddccd07-49e4-42c3-9ecc-27f047078f84@googlegroups.com> <XnsA49C7649C3FD7eejj99@194.109.133.246> <fdb99a69-c853-4c3a-baca-b2de2d605a12@googlegroups.com> <fafb0168-4124-415e-9f02-7775f0256619@googlegroups.com> <XnsA49C98B56DF71eejj99@194.109.133.246>

Show all headers | View raw


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?

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.

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

var i = arr.length;
while (i--) {
// do something with arr[i]
}

and will walk through the array from element arr[arr.length-1] to element 
arr[0].

In the for loops, you can also use prefix style decrement, eg --i as the 
third element of the for expression, however you can not do that when 
using the while loop, as it causes the loop execution to terminate one 
iteration early.

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