How to shuffle an Array in Javascript
Posted on October 9, 2021
My preferred function to shaffle an array:
const shuffle = arr => {
const array = arr.slice()
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]]
}
return array
}
shuffle([1, 4, 7, 8])