Coding since 11yo, that makes it over 30 years now ~~~
Have a PhD in Comp Sci ~~~
Love to go on bike tours ~~~
I try to stay as generalist as I can in this crazy wide place coding is at now.
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
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
ES6
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)