Skip to content

Latest commit

 

History

History
19 lines (17 loc) · 399 Bytes

shuffle-array.md

File metadata and controls

19 lines (17 loc) · 399 Bytes
title description author tags
Shuffle Array
Shuffles an Array.
loxt-nixo
array,shuffle
function shuffleArray(array) {
    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]];
    }
}

// Usage:
const array = [1, 2, 3, 4, 5];
shuffleArray(array); // Shuffles `array` in place