Skip to content

Commit 2bd937e

Browse files
committed
Shuffle array of elements
1 parent 8362552 commit 2bd937e

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

JavaScript/Fisher-Yates shuffle.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function shuffle(array) {
2+
for (let i = array.length - 1; i > 0; i--) {
3+
let j = Math.floor(Math.random() * (i + 1)); // random index from 0 to i
4+
[array[i], array[j]] = [array[j], array[i]]; // swap elements
5+
}
6+
}

0 commit comments

Comments
 (0)