scratch-www/src/lib/shuffle.js
2018-01-30 11:53:12 -05:00

19 lines
481 B
JavaScript

/*
* Function that shuffles an array using a Fisher-Yates shuffle.
*/
module.exports.shuffle = arr => {
let i = 0;
let j = 0;
let temp = null;
if (arr) {
const tempArray = arr.slice(0);
for (i = arr.length - 1; i > 0; i -= 1) {
j = Math.floor(Math.random() * (i + 1));
temp = tempArray[i];
tempArray[i] = tempArray[j];
tempArray[j] = temp;
}
return tempArray;
}
return arr;
};