Created
March 25, 2021 20:16
-
-
Save unicornware/a702702e633db14f74facbddf2660f1d to your computer and use it in GitHub Desktop.
Shuffle 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
| import type { ANYTHING } from '@flex-development/json' | |
| /** | |
| * Shuffle an array. | |
| * | |
| * @param {ANYTHING[]} array - Array to shuffle | |
| * @return {ANYTHING[]} Shuffled array | |
| */ | |
| const shuffle = (array: ANYTHING[]): typeof array => { | |
| let c = array.length - 1 | |
| for (c; c > 0; c--) { | |
| const b = Math.floor(Math.random() * (c + 1)) | |
| const a = array[c] | |
| array[c] = array[b] | |
| array[b] = a | |
| } | |
| return array | |
| } | |
| export default shuffle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment