// shuffle.js
// This module provides functions that will shuffle an array.
// The function is provided in two flavors.
//
// shuffle(array) -- shuffles array in place. No value is returned
//
// getShuffledArray(array) -- does not modify array. A shallow copy of array is made
//                            and shuffled. This shuffled copy is returned by the function

function shuffle(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]];
    }
}

function getShuffledArray(array) {
    // Pure function that returns a shuffled version of "array".
    // The original array is unmodified.
    let shuffledArray = [... array];
    shuffle(shuffledArray);
    return shuffledArray;
}

export {shuffle, getShuffledArray};