Skip to content

Instantly share code, notes, and snippets.

@nondanee
Last active September 9, 2018 02:32
Show Gist options
  • Save nondanee/c8a5f6964ad460b828583b194519517e to your computer and use it in GitHub Desktop.
Save nondanee/c8a5f6964ad460b828583b194519517e to your computer and use it in GitHub Desktop.
where to eat
const menu = [
{
name: 'YOSHINOYA 22',
weight: 4
},
{
name: 'YOSHINOYA 24',
weight: 4
},
{
name: 'MCDONALDS 30',
weight: 3
},
{
name: 'MCDONALDS 40',
weight: 3
}
]
function randomChoice(menu){
let array = []
menu.forEach((item) => {
array = array.concat(Array.apply(null, {length: item.weight}).map(() => item.name))
})
return array[Math.floor(Math.random() * array.length)]
}
console.log(randomChoice(menu))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment