I'm going through javascript.info and doing the practice tasks for Array Methods. This one is "Shuffle an array" and the instructions are:
Write the function shuffle(array) that shuffles (randomly reorders) elements of the array.
I couldn't figure it out so I looked it up but now im struggling to understand it. What does the ".5 -" do in the line .5 - Math.random
?
let arr = [1, 2, 3];
function shuffle(array) {
return arr.sort((a, b) => .5 - Math.random())
}
console.log(shuffle(arr))