Skip to content

Commit 1b0537d

Browse files
authored
Update functions.js
1 parent 4e635ab commit 1b0537d

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

functions.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,17 @@ function randItem(items) {
1717
function toPrecision(number, precision) {
1818
return +((number).toFixed(precision));
1919
}
20+
21+
// shuffles items and returns a new array. Based on the modern Fisher-Yates shuffle algorithm
22+
function shuffleItems(items) {
23+
return items.reduce((result, item, index) => {
24+
if (index === items.length - 1) {
25+
return result;
26+
}
27+
28+
const jindex = Math.floor(Math.random() * (items.length - 1 - index + 1)) + index;
29+
[result[index], result[jindex]] = [result[jindex], result[index]];
30+
31+
return result;
32+
}, items);
33+
}

0 commit comments

Comments
 (0)