最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Shortest code to reverse array - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited May 22, 2018 at 22:32 Patrick Roberts 52k10 gold badges117 silver badges163 bronze badges asked May 22, 2018 at 22:07 MariuszMariusz 1492 silver badges8 bronze badges 4
  • 1 How is sort order determined, here? Couldn't you just leave out the ()=>1? – Daedalus 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 – Mariusz Commented May 22, 2018 at 22:13
  • 3 array.reverse() .. am I missing something?? – mhodges Commented May 22, 2018 at 22:14
  • didnt write it too ... i can't use reverse() – Mariusz Commented May 22, 2018 at 22:15
Add a ment  | 

2 Answers 2

Reset to default 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)

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

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

发布评论

评论列表(0)

  1. 暂无评论