admin管理员组文章数量:1334697
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()
.
-
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
2 Answers
Reset to default 9You 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)
本文标签: javascriptShortest code to reverse arrayStack Overflow
版权声明:本文标题:javascript - Shortest code to reverse array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742341302a2456656.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论