diff --git a/index.js b/index.js index 19d35b4b..aa7bb56a 100644 --- a/index.js +++ b/index.js @@ -4,19 +4,21 @@ const { fifaData } = require('./fifa.js') /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 1: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Practice accessing data by console.log-ing the following pieces of data note. - 💡 HINT: You may want to filter the data first 😉*/ - +const fifaData2014 = fifaData.filter(function(item){ + return item.Year === 2014 && item.Stage === 'Final'; +}) +console.log('1:', fifaData2014); //(a) Home Team name for 2014 world cup final - +console.log('1a:', fifaData2014[0]['Home Team Name']); //(b) Away Team name for 2014 world cup final - +console.log('1b:', fifaData2014[0]['Away Team Name']); //(c) Home Team goals for 2014 world cup final - +console.log('1c:', fifaData2014[0]['Home Team Goals']); //(d) Away Team goals for 2014 world cup final - +console.log('1d:', fifaData2014[0]['Away Team Goals']); //(e) Winner of 2014 world cup final */ - +console.log('1e:', fifaData2014[0]['Win conditions']); /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 2: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Use getFinals to do the following: @@ -26,10 +28,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(data) { + const allFinals = data.filter(function(item){ + return item.Stage === 'Final'; + }) + return allFinals; } - +console.log('2:', getFinals(fifaData)); /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 3: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 @@ -38,10 +43,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(data, getFinalsCB) { + return getFinalsCB(data).map(item => item.Year); } - +console.log('3:', getYears(fifaData, getFinals)); /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 4: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 @@ -52,11 +57,12 @@ 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(data, getFinalsCB) { + return getFinalsCB(data).map(item => (item['Home Team Goals'] > item['Away Team Goals']) ? + item['Home Team Name'] : item['Away Team Name']); } - +console.log('4:', getWinners(fifaData, getFinals)); /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 5: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Use the higher-order function getWinnersByYear to do the following: @@ -69,11 +75,14 @@ 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(fifadata, getFinalsCB, getYearsCB, getWinnersCB) { + const winners = getWinners(fifadata, getFinals); + const years = getYears(fifadata, getFinals); + const Final = getFinals(fifaData, getFinals) + return winners.map((item, index)=>`In ${years[index]}, ${item} won the world cup!`); } - +console.log('5:', getWinnersByYear(fifaData, getYears, getWinners)); /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 6: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Use the higher order function `getAverageGoals` to do the following: @@ -89,11 +98,14 @@ Use the higher order function `getAverageGoals` to do the following: */ -function getAverageGoals(/* code here */) { - /* code here */ - } - +function getAverageGoals(fifaData) { + const averageHomeGoals = fifaData.reduce(function(acc, item){ + return acc + item['Home Team Goals'] + item['Away Team Goals']; + }, 0) + return(averageHomeGoals / fifaData.length).toFixed(2); + } +console.log('6:', getAverageGoals(fifaData)); /// 🥅 STRETCH 🥅 ///