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