diff --git a/index.js b/index.js index 19d35b4b..87635258 100644 --- a/index.js +++ b/index.js @@ -7,16 +7,30 @@ Practice accessing data by console.log-ing the following pieces of data note. 💡 HINT: You may want to filter the data first 😉*/ +const finals2014 = fifaData.filter(function(match){ + return match.Year === 2014 && match.Stage === 'Final' +}); +console.log(finals2014); + //(a) Home Team name for 2014 world cup final +console.log(finals2014[0]["Home Team Name"]); + //(b) Away Team name for 2014 world cup final +console.log(finals2014[0]["Away Team Name"]); + //(c) Home Team goals for 2014 world cup final +console.log(finals2014[0]["Home Team Goals"]); + //(d) Away Team goals for 2014 world cup final +console.log(finals2014[0]["Away Team Goals"]); + //(e) Winner of 2014 world cup final */ +console.log(finals2014[0]["Win conditions"]); /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 2: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Use getFinals to do the following: @@ -26,9 +40,15 @@ 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) { + let finalData = [] + for (let i=0; i item.Year) } @@ -52,8 +73,11 @@ 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, getFinals) { + let finals = getFinals(data) + let winners = finals.map(teams => { + return teams['Home Team Goals'] > teams['Away Team Goals'] ? teams['Home Team Name'] : teams['Away Team Name'] }); + return winners } @@ -69,8 +93,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(data, getFinals, getYears) { + const winnerYear = []; + const year = getYears(data, getFinals); + const country = getWinners(data, getFinals); + for (let x = 0; x < year.length; x++) { + winnerYear.push(`In ${year[x]}, ${country[x]} won the world cup!`) + } + return winnerYear } @@ -89,9 +119,17 @@ Use the higher order function `getAverageGoals` to do the following: */ -function getAverageGoals(/* code here */) { - /* code here */ - } +function getAverageGoals(data) { + let homeGoals = data.reduce((x, y) => { + return x += y["Home Team Goals"]; + }, 0) / data.length; + let awayGoals = data.reduce((x, y) => { + return x += y["Away Team Goals"]; + }, 0) / data.length; + return (homeGoals+awayGoals).toFixed(2); +}; + +console.log(getAverageGoals(fifaData));