Skip to content

Instantly share code, notes, and snippets.

@mrf345
Last active March 6, 2018 06:46
Show Gist options
  • Save mrf345/00f81969be1cbb179a060f5c9fa03df7 to your computer and use it in GitHub Desktop.
Save mrf345/00f81969be1cbb179a060f5c9fa03df7 to your computer and use it in GitHub Desktop.
Choose randomly from an Array ES6
const Choice = (list) => {
// to choose randomly from an Array
let powerOfLength = Math.floor(list.length / 10)
if (powerOfLength <= 0) powerOfLength = 1
let toReturn = list[Math.floor(Math.random() * (10 * powerOfLength))]
return toReturn ? toReturn : Choice(list)
}
Choice([1, 2, 3, 4, ,5])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment