.map((_,i) : "i" will be the index of the current element, we don't need to work with the value here
We return a value located inside the input, at the following index:
If we are in an odd space, the targeted index will be i/2 + true * (a.length / 2 + 0.5). Thanks to coercion, "true" will be translated to 1, hence we will fetch the (a.length / 2 + 0.5)+i/2th index
Else if we are in an even space, the target will be i/2 + false * (a.length / 2 + 0.5), translated to i/2 + 0 * (a.length / 2 + 0.5) (hence, i/2)
Taking into account that the input is an array, you could save some bytes by replacing [...a] with just a (for golf purposes).
Apart from that, the solution seems really specific to the problem, and doesn't work for different arrays. For example:
f(['ace','two','three','four','five','six','seven','eight'])//["ace", "four", "two", "five", "three", "six", "four", "seven"]// there are 2 "four" and no "eight"
A bit of golf:
It works like this:
.map((_,i)
: "i" will be the index of the current element, we don't need to work with the value herei/2 + true * (a.length / 2 + 0.5)
. Thanks to coercion, "true" will be translated to1
, hence we will fetch the(a.length / 2 + 0.5)+i/2
th indexi/2 + false * (a.length / 2 + 0.5)
, translated toi/2 + 0 * (a.length / 2 + 0.5)
(hence,i/2
)The return will then be the following:
Taking into account that the input is an array, you could save some bytes by replacing
[...a]
with justa
(for golf purposes).Apart from that, the solution seems really specific to the problem, and doesn't work for different arrays. For example:
Thanks a lot for the input! The problem was in the
2.5
usage, which had to be replaced witha.length/2-.5
. I updated my answer!You can shorten the index computation to: