Hello friends! We need a collective mind.

The second day I struggle with the task. You must write the reverse function without using Array.prototype.reverse () in 30 characters. I got to this option, and it looks like this is my limit in JS knowledge. (This option is not suitable as solution.length> 30)

let reverse=a=>[...a].map(a.pop,a) 

In short, [... a] - creates a copy of the array, we go through each element with the help of map and using the cb function a.pop, with the context a, return the reverse sequence of the array a.

Who knows the way to cheat, share it, but better tell me where to dig.

  • But are there any additional features like "you can not rename reverse to r "? xDD - MedvedevDev
  • because nobody said that the variable (function) cannot be global, then remove the let and we get 30 :) - Alexey Shimansky
  • @ Alexey Shimansky, really ... why keep such a wonderful function in a non-global area xDD - MedvedevDev
  • @ Alexey Shimansky thanks! I did not know this feature. I will remember. Thank you friend! - ioahn

1 answer 1

You can use sort . The bottom line is that the function compares two values ​​at the expense of compareFunction , which can be specified. And if 1 is returned in compareFunction , then the elements are swapped. From here you can dance. That is, in the end, not to compare the elements, but just stupidly always return 1. Well, since we do not need to compare the elements, we omit them (arguments) in the input data

 let reverse=a=>a.sort(a=>1); // 29 // let reverse=a=>a.sort(a=>1); // 28 - даже так работает let myArray = [1, 5, 88, 3, 0, -666]; console.log(reverse(myArray)); 

  • The implementation-defined is obtained (it will work somewhere, somewhere it will not). - Vladimir Gamalyan
  • @VladimirGamalyan are talking about different YAP, different input data or what? - Alexey Shimansky
  • Different implementations of javascript. This is not a clear comparison of the intergroup (see below). , ibid, below, describes why it is not consistent (does not fall under a =CF a (reflexivity) , because for identical values ​​it returns non-zero). - Vladimir Gamalyan
  • @VladimirGamalyan is the same code-golf question-answer. And checking on the standards of golf. This is clearly not the code that should be used on combat vehicles. As a result, the code, in general, works and solves the problem. - Alexey Shimansky