Last active
January 20, 2020 17:51
-
-
Save gabrielrufino/d535826bf4e4bd5efbd6b82a46650a60 to your computer and use it in GitHub Desktop.
Shuffle the elements of an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const shuffle = require('./shuffle') | |
const example = [1, 2, 3] | |
const shuffled = shuffle(example) | |
/* | |
shuffled can be any of these: | |
- [1, 2, 3] | |
- [1, 3, 2] | |
- [2, 1, 3] | |
- [2, 3, 1] | |
- [3, 1, 2] | |
- [3, 2, 1] | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function shuffle(array) { | |
const copy = [...array] | |
return copy.sort(() => Math.random() - 0.5) | |
} | |
module.exports = shuffle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment