Created
March 15, 2019 10:52
-
-
Save avermeulen/2fd024da6b522d260b55d40f4514d62f to your computer and use it in GitHub Desktop.
Create list of random fruits to help explaining concepts
This file contains hidden or 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 fruits = [ | |
'apples', | |
'pears', | |
'grapes', | |
'peaches', | |
'oranges' | |
]; | |
var list = []; | |
for(var i=0;i<1000;i++){ | |
var num = Math.floor((Math.random() * 5)); | |
list.push({ | |
name : fruits[num], | |
price : Number((Math.random() * 10).toFixed(2)) | |
}); | |
} | |
var totals = {}; | |
for(var i=0;i<list.length;i++){ | |
var fruit = list[i]; | |
if (totals[fruit.name] === undefined) { | |
totals[fruit.name] = 0; | |
} | |
totals[fruit.name] += fruit.price; | |
} | |
console.log(totals); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment