Last active
September 9, 2018 02:32
-
-
Save nondanee/c8a5f6964ad460b828583b194519517e to your computer and use it in GitHub Desktop.
where to eat
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 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