Given the following code
var arr = [1, 2, 3, 4, 5]; function compareRandom(a, b) { return Math.random() - 0.5; } arr.sort(compareRandom); alert( arr ); // элементы в случайном порядке, например [3,5,1,2,4] Each time it places the elements of the array in a different order, but why not the same effect if we remove -0.5 from the function?
var arr = [1, 2, 3, 4, 5]; function compareRandom(a, b) { return Math.random(); } arr.sort(compareRandom); alert( arr ); // [1, 2, 3, 4, 5]