diff --git a/index.js b/index.js index 28697d2a..c65a8520 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,5 @@ + + const { fifaData } = require('./fifa.js') // ⚽️ M V P ⚽️ // @@ -7,15 +9,29 @@ Practice accessing data by console.log-ing the following pieces of data note. 💡 HINT: You may want to filter the data first 😉*/ -//(a) Home Team name for 2014 world cup final +var filterMatch +for (var i = 0; i < fifaData.length; i++) { + if (fifaData[i].Year === 2014 && fifaData[i].Stage == "Final") { + filterMatch = fifaData[i] + } +} -//(b) Away Team name for 2014 world cup final -//(c) Home Team goals for 2014 world cup final +var result = fifaData.filter(match => match["Year"] === 2014 && match["Stage"] === "Final")[0]; +//(a) Home Team name for 2014 world cup final +//(b) Away Team name for 2014 world cup final +console.log(result['Away Team Name']); +//(c) Home Team goals for 2014 world cup final +console.log(result['Home Team Goals']); //(d) Away Team goals for 2014 world cup final - +console.log(result['Away Team Goals']); //(e) Winner of 2014 world cup final */ +if (result['Home Team Goals'] > result['Away Team Goals']) { + console.log(result['Home Team Name']); +} else { + console.log(result['Away Team Name']); +} /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 2: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 @@ -26,11 +42,11 @@ 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) { + return data.filter(match => match['Stage'] === 'Final'); +} +console.log(getFinals(fifaData)) /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 3: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Use the higher-order function called getYears to do the following: @@ -38,10 +54,12 @@ 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, finals) { + var finalsList = finals(data); + return finalsList.map(match => match['Year']); } +console.log(getYears(fifaData,getFinals)) /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 4: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 @@ -52,10 +70,13 @@ 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,finals) { + var finalsList = finals(data); + return finalsList.map(match => (match['Home Team Goals'] > match['Away Team Goals']) ? match['Home Team Name'] : match['Away Team Name']); } +console.log(getWinners(fifaData,getFinals)) + /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 5: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 @@ -69,11 +90,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,finals,years,winners) { + var winners = winners(data,finals) + var theYears = years(data,finals) + return winners.map((x,i) => `In ${theYears[i]}, ${x} won the world cup!`); +} +getWinnersByYear(fifaData,getFinals,getYears,getWinners) /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 6: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Use the higher order function getAverageGoals to do the following: @@ -88,10 +112,17 @@ Use the higher order function getAverageGoals to do the following: */ -function getAverageGoals(/* code here */) { - /* code here */ - } - +function getAverageGoals(data) { + var homeGoals = data.reduce((x, y) => { + return x += y["Home Team Goals"]; + }, 0) / data.length; + var awayGoals = data.reduce((x, y) => { + return x += y["Away Team Goals"]; + }, 0) / data.length; + return (homeGoals+awayGoals).toFixed(2); +}; + +console.log(getAverageGoals(fifaData));