re: Daily Challenge #286 - Reverse It Quickly! VIEW POST

re: ES6 a=>[...a].map(a.pop,a)

Nice solution!


would also work since a is said to already be an array

Edit: Um, nup Will. You're on to it Birand. Got the feeling the spread was there for a reason :|

In case anyone else is fooled into thinking you don't need to clone the original array in this case...
map is smart enough to handle some mutations of the input array, like deleting the currently iterated value, but it isn't exactly making a copy of the array before the call.
In this case, a is being shrunk from the end as it's iterated.
Once the iterator gets to halfway, the pop function has shaved as many entries off the end, so the map function early exits.
Interestingly, the map function sets the length of the output array to be the same as the original array, so my function returns an incomplete array, with some of the values you'd expect in an array being missing properties, a little like in the result of Array(6)

> revHalf([1,2,3,4,5,6])
< [6, 5, 4, empty × 3]
<    0: 6
<    1: 5
<    2: 4
<    length: 6
<    __proto__: Array(0)

// You'd expect keys 3, 4, 5 too
Code of Conduct Report abuse