Last active
March 6, 2018 06:46
-
-
Save mrf345/00f81969be1cbb179a060f5c9fa03df7 to your computer and use it in GitHub Desktop.
Choose randomly from an Array ES6
This file contains 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 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