Categories
Shuffles an array using the Fisher-Yates algorithm.
Contributed by @itsbrunodev
function shuffle(arr) { for (let i = arr.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [arr[i], arr[j]] = [arr[j], arr[i]]; } return arr; }
const arr = [1, 2, 3, 4, 5]; shuffle(arr); // e.g. [5, 3, 2, 4, 1]