From 93b03696622fce03470ee1ac4387f7e37c2d83cf Mon Sep 17 00:00:00 2001 From: Terri Archer Date: Sun, 28 Aug 2022 16:44:40 -0400 Subject: [PATCH] completed coursework --- index.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 19d35b4b..c4898546 100644 --- a/index.js +++ b/index.js @@ -7,14 +7,40 @@ Practice accessing data by console.log-ing the following pieces of data note. 💡 HINT: You may want to filter the data first 😉*/ +const fifa2014 = [{}] +fifaData.filter((element) => { + if(element.Year === 2014){ + fifa2014.push(element) + } +}) + //(a) Home Team name for 2014 world cup final +const HomeName2014 = [] +fifa2014.forEach((element) => { + HomeName2014.push(element['Home Team Name']) +}) + //(b) Away Team name for 2014 world cup final +const AwayName2014 = [] +fifa2014.forEach((element) => { + AwayName2014.push(element['Away Team Name']) +}) //(c) Home Team goals for 2014 world cup final +const HomeGoal2014 = [] +fifa2014.forEach((element) => { + HomeGoal2014.push(element['Home Team Goals']) +}) //(d) Away Team goals for 2014 world cup final +const AwayGoal2014 = [] +fifa2014.forEach((element) => { + AwayGoal2014.push(element['Away Team Goals']) +}) + + //(e) Winner of 2014 world cup final */ @@ -26,8 +52,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(arr) { + const finals = arr.filter(function(item){ + return item.Stage === 'Final'; + }) + return(finals) } @@ -38,8 +67,9 @@ 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(arr,cb) { + return cb(arr).map(item => item.Year); + } @@ -52,8 +82,17 @@ 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(arr,cb) { + const winner = [] + cb(arr).forEach((element) => { + if(element['Home Team Goals'] > element['Away Team Goals']){ + winner.push(element['Home Team Name']) + } + else { + winner.push(element['Away Team Name']) + } + }) + return(winner) } @@ -69,12 +108,16 @@ 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(arr,finalscb,yearscb,winnercb) { + const whoWon = winnercb(arr,finalscb) + const whatYear = yearscb(arr,finalscb) + return whoWon.map((element, index) => `In ${whatYear[index]}, ${element} won the world cup!` ) + } + /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 6: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Use the higher order function `getAverageGoals` to do the following: 1. Receive a callback function as a parameter that will take `getFinals` (from task 2) as an argument; ensure you pass in `fifaData` as its argument @@ -89,8 +132,11 @@ Use the higher order function `getAverageGoals` to do the following: */ -function getAverageGoals(/* code here */) { - /* code here */ +function getAverageGoals(finalscb) { + const averageHomeGoals = finalscb.reduce(function(acc,element) { + return acc + element['Home Team Goals'] + element['Away Team Goals']; + }, 0) + return (averageHomeGoals / finalscb.length).toFixed(2); }