Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortest code to reverse array

I need to find the shortest code for reversing an array and at the moment I have this:

weirdReverse=a=>a.sort(()=>1)

This is a codewars challenge and the length of this is 29 bytes. I need to have a length <=28 so I need to remove 1 byte and I can't change this part:

weirdReverse=a=>a

And I can't use reverse().

like image 339
Mariusz Avatar asked Dec 23 '22 07:12

Mariusz


2 Answers

You can golf one byte off your anonymous arrow function by specifying an unused parameter instead of () to indicate no named parameters, bringing the total down to 28 bytes:

weirdReverse=a=>a.sort(_=>1)
like image 85
Patrick Roberts Avatar answered Dec 28 '22 23:12

Patrick Roberts


You can use a named parameter instead of braces, like so:

weirdReverse=a=>a.sort(b=>1)

like image 41
mhodges Avatar answered Dec 28 '22 22:12

mhodges