How do I reverse an array in JavaScript in 16 characters or less without .reverse()?

后端 未结 1 1249
失恋的感觉
失恋的感觉 2021-01-23 22:31

I\'m trying to solve a challenge on Codewars which requires you to reverse an array in JavaScript, in 16 characters or less. Using .reverse() is not an option.

相关标签:
1条回答
  • 2021-01-23 23:12

    I'd like to give you a hint, without giving you the answer:

    You're close, but you can save characters by not using something you need to add in your code.

    By adding the thing you won't use, you can remove ().


    Spoiler (answer):

    // Note: this only really works for this specific case.
    // Never EVER use this in a real-life scenario.
    
    var a = [1,2,3,'a','b','c',[]]
    
    weirdReverse=a=>a.sort(x=>1)
    //                     ^ That's 1 character shorter than ()
    
    console.log(weirdReverse(a))

    0 讨论(0)
提交回复
热议问题