I am a beginner in Javascript , and now i am reading a book , where i am confused with one of the tasks solution.
var arr = [1, 2, 3, 4, 5] :
With the help of sort function we have to get an random order in an array. Here is the authors solution `
function compareRandom(a, b) {
return Math.random() - 0.5 ;
}
var arr = [1,2,3,4];
arr.sort(compareRandom);
alert( arr ); // elements are in a random order ` [3,5,1,2,4]
So i cant understand how the function works , its realization step by step. IS there someone who can explain it to me???