Skip to content

Commit 3e4f801

Browse files
author
tmurphy21
committed
add shuffle array
1 parent 11c76c0 commit 3e4f801

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Array/Shuffle.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function shuffle(arr = []) {
2+
const copy = arr.slice();
3+
4+
let currentIndex = arr.length;
5+
let randomIndex;
6+
while (currentIndex) {
7+
randomIndex = Math.floor(Math.random() * currentIndex);
8+
currentIndex--;
9+
10+
[copy[currentIndex], copy[randomIndex]] = [
11+
copy[randomIndex], copy[currentIndex]];
12+
};
13+
14+
return copy;
15+
};

0 commit comments

Comments
 (0)