scratch-www/src/lib/shuffle.js

20 lines
481 B
JavaScript
Raw Normal View History

2016-06-13 09:40:32 -04:00
/*
* Function that shuffles an array using a Fisher-Yates shuffle.
*/
module.exports.shuffle = arr => {
let i = 0;
let j = 0;
let temp = null;
2016-06-13 09:40:32 -04:00
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;
2016-06-13 09:40:32 -04:00
}
return arr;
2016-06-13 09:40:32 -04:00
};