Skip to content

Instantly share code, notes, and snippets.

@unicornware
Created March 25, 2021 20:16
Show Gist options
  • Select an option

  • Save unicornware/a702702e633db14f74facbddf2660f1d to your computer and use it in GitHub Desktop.

Select an option

Save unicornware/a702702e633db14f74facbddf2660f1d to your computer and use it in GitHub Desktop.
Shuffle an array
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