Skip to content

Instantly share code, notes, and snippets.

@avermeulen
Created March 15, 2019 10:52
Show Gist options
  • Save avermeulen/2fd024da6b522d260b55d40f4514d62f to your computer and use it in GitHub Desktop.
Save avermeulen/2fd024da6b522d260b55d40f4514d62f to your computer and use it in GitHub Desktop.
Create list of random fruits to help explaining concepts
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