Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
finished array methods and callbacks
  • Loading branch information
amandasihakom committed Feb 15, 2023
commit 6f22d00a19a0a7dacbee43acba1ea2e6b6321be0
51 changes: 32 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
Expand All @@ -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: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
Expand All @@ -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));



Expand All @@ -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))}



Expand All @@ -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));



Expand Down