5

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().

4
  • 1
    How is sort order determined, here? Couldn't you just leave out the ()=>1? Commented May 22, 2018 at 22:11
  • Sorry im dumb , i need to reverse it i just look on sort() and i write i need to sort it , description is changed now Commented May 22, 2018 at 22:13
  • 3
    array.reverse() .. am I missing something?? Commented May 22, 2018 at 22:14
  • didnt write it too ... i can't use reverse() Commented May 22, 2018 at 22:15

2 Answers 2

9

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)
Sign up to request clarification or add additional context in comments.

3 Comments

Bro you are great , i didn't know about that
I just dont understand why i always get -1 when i create a topic , its sad :(
@Mariusz I haven't looked at your other questions, but this one barely qualifies as on-topic, and the answer is not going to be useful to anyone finding this on google, so there's no value-added for Stack Overflow to have this question on their site. I just happen to be an active participant on Programming Puzzles & Code Golf, so I knew the answer to this.
4

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

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

1 Comment

Thanks you both good to know i dont need to use () and i can change it to something different

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.