Skip to content

completed array methods and callbacks #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
59 changes: 45 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@ const { fifaData } = require('./fifa.js')
/* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 1: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
Practice accessing data by console.log-ing the following pieces of data note, you may want to filter the data first 😉*/

let finals2014 = fifaData.filter(
(match) => {
return match.Year === 2014 && match.Stage === 'Final'
})

//(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: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
Expand All @@ -24,10 +34,19 @@ 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 < data.length; i++){
// if (data[i].Stage === 'Final'){
// finalData.push(data[i])
// }
// } return finalData
const finalData = data.filter(
(match) => { return match.Stage === 'Final'
}); return finalData
}

// console.log(getFinals(fifaData))


/* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 3: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
Expand All @@ -36,8 +55,11 @@ Use the higher-order function called getYears to do the following:
2. Receive a callback function getFinals from task 2
3. Return an array called years containing all of the years in the getFinals data set*/

function getYears(/* code here */) {
/* code here */
function getYears(array, getFinalsCB) {
let years = getFinalsCB(array).map(
(match) =>{
return match.Year
}); return years
}


Expand All @@ -49,8 +71,11 @@ Use the higher-order function getWinners to do the following:
3. Determines the winner (home or away) of each `finals` game.
4. Returns the names of all winning countries in an array called `winners` */

function getWinners(/* code here */) {
/* code here */
function getWinners(array, getFinalsCB) {
let winners = getFinalsCB(array).map(
(match) => {return match['Home Team Goals'] > match['Away Team Goals'] ?
match['Home Team Name'] : match['Away Team Name']
}); return winners
}


Expand All @@ -66,12 +91,13 @@ 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(array, getFinalsCB, getYearsCB, getWinnersCB) {
let wonWorldCup = []
for (let i =0; i < getFinalsCB(array).length; i++){
wonWorldCup.push(`In ${getYearsCB(array)[i]}, ${getWinnersCB(array)[i]} won the world cup!`)}
return wonWorldCup
}



/* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 6: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀
Use the higher order function getAverageGoals to do the following:
1. Receive the callback function getFinals from task 2 ensure you pass in the data as an argument
Expand All @@ -82,13 +108,18 @@ Use the higher order function getAverageGoals to do the following:
Example of invocation: getAverageGoals(getFinals(fifaData));
*/

function getAverageGoals(/* code here */) {
/* code here */
function getAverageGoals(array) {
let averageGoals = array.reduce(
(accumulator, goals) => {
let sum = accumulator + goals['Home Team Goals'] + goals['Away Team Goals']
return sum
}
,0);
let average = (averageGoals / array.length).toFixed(2)
return average
}




/// 🥅 STRETCH 🥅 ///

/* 💪💪💪💪💪 Stretch 1: 💪💪💪💪💪
Expand Down
Loading