diff --git a/index.js b/index.js index 28697d2a..977ab448 100644 --- a/index.js +++ b/index.js @@ -26,11 +26,13 @@ Use getFinals to do the following: 💡 HINT - you should be looking at the stage key inside of the objects */ -function getFinals(/* code here */) { - /* code here */ +function getFinals(array) { + const allFinals = array.filter(function(item){ + return item.Stage === 'Final'; + }) + return allFinals; } - - +// console.log(getFinals(fifaData)); /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 3: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Use the higher-order function called getYears to do the following: @@ -38,10 +40,10 @@ Use the higher-order function called getYears to do the following: 2. Receive a callback function as the second parameter that will take getFinals from task 2 as an argument 3. Return an array called years containing all of the years in the getFinals data set*/ -function getYears(/* code here */) { - /* code here */ -} +function getYears(array, getFinalsCB) { + return getFinalsCB(array).map(item => item.Year); +} /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 4: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 @@ -52,8 +54,9 @@ Use the higher-order function getWinners to do the following: 💡 HINT: Don't worry about ties for now (Please see the README file for info on ties for a stretch goal.) 4. Returns the names of all winning countries in an array called `winners` */ -function getWinners(/* code here */) { - /* code here */ +function getWinners(array, getFinalsCB) { + return getFinalsCB(array).map(item => item['Home Team Goals'] > item['Away Team Goals'] ? + item ['Home Team Name'] : item ['Away Team Name']); } @@ -69,8 +72,12 @@ Use the higher-order function getWinnersByYear to do the following: 💡 HINT: the strings returned need to exactly match the string in step 4. */ -function getWinnersByYear(/* code here */) { - /* code here */ +function getWinnersByYear(array, getFinalsCB, getYearsCB, getWinnersCB) { + const winners = getWinnersCB(array, getFinals) + const years = getYearsCB(array, getFinals) + + + return winners.map((item, index) =>`In ${years[index]}, ${item} won the world cup!`); } @@ -88,8 +95,11 @@ Use the higher order function getAverageGoals to do the following: */ -function getAverageGoals(/* code here */) { - /* code here */ +function getAverageGoals(array) { + const avgHomeGoals = array.reduce(function(accumulator, item){ + return accumulator + item['Home Team Goals'] + item ['Away Team Goals']; + }, 0) + return (avgHomeGoals / array.length).toFixed(2); }