Last active
July 10, 2020 16:32
-
-
Save sandrabosk/4faa55d3c48f5b6542c31f2af2b6f38e to your computer and use it in GitHub Desktop.
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
// 1: Calculate the total population in the given array (`data`): | |
const data = [ | |
{ | |
country: 'USA', | |
pop: 340 | |
}, | |
{ | |
country: 'France', | |
pop: 133 | |
}, | |
{ | |
country: 'Bosnia', | |
pop: 5 | |
} | |
]; | |
const totalPop = data.reduce((accum, currentValue) => accum + currentValue.pop, 0); | |
console.log(`totalPop: ${totalPop}`); // totalPop: 478 | |
// 2: Given a menu of foods and their calories, calculate the average number of calories for the entire list. | |
const menu = [ | |
{ name: 'Carrots', calories: 150 }, | |
{ name: 'Steak', calories: 350 }, | |
{ name: 'Broccoli', calories: 120 }, | |
{ name: 'Chicken', calories: 250 }, | |
{ name: 'Pizza', calories: 520 } | |
]; | |
const totalCalories = menu.reduce((acc, currValue) => acc + currValue.calories, 0); | |
const avgCalories = totalCalories / menu.length; | |
console.log(avgCalories); // => 278 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment