0
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8];
arr.sort(() => Math.random() > 0.5 ? 1 : -1);

Please explain what is happening by the above lines of code, why we are returning 1 or -1 and what is the use of returning them.

  • 1
    Have you looked at documentation for the `sort` method? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort, particularly for the `compareFn` argument? – ndc85430 Nov 06 '22 at 16:16

0 Answers0