From 6f22d00a19a0a7dacbee43acba1ea2e6b6321be0 Mon Sep 17 00:00:00 2001 From: Sihakom Date: Wed, 15 Feb 2023 14:13:15 -0800 Subject: [PATCH 1/4] finished array methods and callbacks --- index.js | 51 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index 19d35b4b..fb9871f0 100644 --- a/index.js +++ b/index.js @@ -6,17 +6,20 @@ const { fifaData } = require('./fifa.js') 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(item){ + return item.Year === 2014 && item.Stage === `Final`; +}); +console.log(finals2014); //(a) Home Team name for 2014 world cup final - +console.log(`task 1a`, finals2014[0][`Home Team Name`]); //(b) Away Team name for 2014 world cup final - +console.log(`task 1b`,finals2014[0][`Away Team Name`]); //(c) Home Team goals for 2014 world cup final - +console.log(`task 1c`,finals2014[0][`Home Team Goals`]); //(d) Away Team goals for 2014 world cup final - +console.log(`task 1d`,finals2014[0][`Away Team Goals`]); //(e) Winner of 2014 world cup final */ - +console.log(`task 1e`, finals2014[0][`Win conditions`]); /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 2: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Use getFinals to do the following: @@ -26,10 +29,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(getFinals(fifaData)); /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 3: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 @@ -38,10 +44,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(getYears(fifaData, getFinals)); /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 4: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 @@ -52,9 +58,10 @@ 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(`task 4`, getWinners(fifaData, getFinals)); @@ -69,9 +76,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(data, getFinals, getYearsCB, getWinnersCB) {{ + const winners = getWinnersCB(data, getFinals); + const years = getYearsCB(data, getFinals); + return winners.map((item, index)=> `In ${years[index]}, ${item} won the world cup!`) } +console.log(getWinnersByYear(fifaData, getYears, getWinners))} @@ -89,10 +99,13 @@ Use the higher order function `getAverageGoals` to do the following: */ -function getAverageGoals(/* code here */) { - /* code here */ +function getAverageGoals(data) { + const averageHomeGoals = data.reduce(function(acc, item){ + return acc + item [`Home Team Goals`] + item[`Away Team Goals`]; + }, 0) + return (averageHomeGoals / data.length).toFixed(2); } - +console.log(`task 6`, getAverageGoals(fifaData)); From 01764d1d9ab83a472260b0f5dcb1e25f794d7e76 Mon Sep 17 00:00:00 2001 From: Sihakom Date: Wed, 15 Feb 2023 17:34:50 -0800 Subject: [PATCH 2/4] Create a CodeGrade submission From 0bab8fc06badfaa75b6f5f62cd3072979ae5d97b Mon Sep 17 00:00:00 2001 From: Sihakom Date: Wed, 15 Feb 2023 20:41:46 -0800 Subject: [PATCH 3/4] finished again --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index fb9871f0..41011c01 100644 --- a/index.js +++ b/index.js @@ -71,7 +71,7 @@ Use the higher-order function getWinnersByYear to do the following: 2. Receive a callback function as the second parameter that will take getFinals from task 2 as an argument 3. Receive a callback function as the third parameter that will take getYears from task 3 as an argument 4. Receive a callback function as the fourth parameter that will take getWinners from task 4 as an argument -5. Return an array of strings that say "In {year}, {country} won the world cup!" +5. Return an array of strings that say "In {year}, {country} won the world cup!" 💡 HINT: the strings returned need to exactly match the string in step 4. */ @@ -81,7 +81,7 @@ function getWinnersByYear(data, getFinals, getYearsCB, getWinnersCB) {{ const years = getYearsCB(data, getFinals); return winners.map((item, index)=> `In ${years[index]}, ${item} won the world cup!`) } -console.log(getWinnersByYear(fifaData, getYears, getWinners))} +console.log(getWinnersByYear(fifaData, getYears, getWinners))}; From 2ce7d629d0423a95ccb3235fd4edb73a1ced4d57 Mon Sep 17 00:00:00 2001 From: Sihakom Date: Wed, 15 Feb 2023 20:47:38 -0800 Subject: [PATCH 4/4] Create a CodeGrade submission