Skip to content

Instantly share code, notes, and snippets.

@mfd
Last active November 16, 2016 18:54
Show Gist options
  • Save mfd/2a8fd9e58c6f46144c576039eee0ff39 to your computer and use it in GitHub Desktop.
Save mfd/2a8fd9e58c6f46144c576039eee0ff39 to your computer and use it in GitHub Desktop.
Get random item from JavaScript array
var items = ["a","e","i","o","u"];
var objResults = {}
for(var i = 0; i < 1000000; i++){
var randomElement = items[Math.floor(Math.random()*items.length)];
if (objResults[randomElement]){
objResults[randomElement]++
}
else {
objResults[randomElement] = 1
}
}
console.log(objResults)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment