diff --git a/index.js b/index.js index 19d35b4b..e84e1e23 100644 --- a/index.js +++ b/index.js @@ -6,16 +6,24 @@ 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 data2014 = fifaData.filter((element) => { + return element.Year === 2014 && element.Stage === "Final"; +}); //(a) Home Team name for 2014 world cup final +console.log(data2014[0]['Home Team Name']); //(b) Away Team name for 2014 world cup final +console.log(data2014[0]['Away Team Name']); //(c) Home Team goals for 2014 world cup final +console.log(data2014[0]['Home Team Goals']); //(d) Away Team goals for 2014 world cup final +console.log(data2014[0]['Away Team Goals']); //(e) Winner of 2014 world cup final */ +console.log(data2014[0]['Win conditions']); /* 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Task 2: 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 @@ -26,8 +34,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(array) { + let finaldata = array.filter((element) => { + return element.Stage === "Final" + }) + return finaldata; } @@ -38,9 +49,15 @@ 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(array, cb) { + let year = []; + cb(array).map((element) => { + year.push(element.Year); + }) + return year; + } +console.log(getYears(fifaData, getFinals)); @@ -52,9 +69,25 @@ 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(array, cb) { + //my solution to get the winner name but it does not pass the test + + // let winners = []; + // cb(array).map((element) => { + // let winnerName = element["Win conditions"].split(" ")[0] ; + // if (winnerName === element["Home Team Name"]) + // { + // winners.push(element["Home Team Name"]); + // } else + // { + // winners.push(element["Away Team Name"]); + // } + // }) + // return winners; + + return cb(array).map(element => element["Home Team Goals"] > element["Away Team Goals"] ? element["Home Team Name"] : element["Away Team Name"]); } +console.log(getWinners(fifaData, getFinals)); @@ -69,8 +102,15 @@ 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 winnerYear = getYearsCB(array, getFinalsCB); + let winnerName = getWinnersCB(array, getFinalsCB); + let finalData = []; + for (let i = 0; i < winnerName.length; i++) + { + finalData.push(`In ${winnerYear[i]}, ${winnerName[i]} won the world cup!`) + } + return finalData; } @@ -89,9 +129,13 @@ Use the higher order function `getAverageGoals` to do the following: */ -function getAverageGoals(/* code here */) { - /* code here */ +function getAverageGoals(array) { + const reducedFinal = array.reduce((accumulator, currentValue) => { + return accumulator + currentValue["Home Team Goals"] + currentValue["Away Team Goals"]; + }, 0); + return (reducedFinal / array.length).toFixed(2); } + console.log(getAverageGoals(fifaData)); @@ -104,12 +148,6 @@ Create a function called `getCountryWins` that takes the parameters `data` and ` Hint: Investigate your data to find "team initials"! Hint: use `.reduce` */ -function getCountryWins(/* code here */) { - - /* code here */ - -} - /* 💪💪💪💪💪 Stretch 2: 💪💪💪💪💪 diff --git a/stretch.js b/stretch.js new file mode 100644 index 00000000..628e6ebf --- /dev/null +++ b/stretch.js @@ -0,0 +1,60 @@ +const { fifaData } = require('./fifa.js') +/// 🥅 STRETCH 🥅 /// + +/* 💪💪💪💪💪 Stretch 1: 💪💪💪💪💪 +Create a function called `getCountryWins` that takes the parameters `data` and `team initials` and returns the number of world cup wins that country has had. + +Hint: Investigate your data to find "team initials"! +Hint: use `.reduce` */ +//i give up on this one + + /* 💪💪💪💪💪 Stretch 2: 💪💪💪💪💪 + Write a function called getGoals() that accepts a parameter `data` and returns the team with the most goals score per appearance (average goals for) in the World Cup finals */ + + + function getGoals(data) { + let highestGoal = 0; + let teamName = ""; + data.map((element) => { + if (element["Home Team Goals"] > highestGoal) + { + highestGoal = element["Home Team Goals"]; + teamName = element["Home Team Name"]; + } + if (element["Away Team Goals"] > highestGoal) + { + highestGoal = element["Away Team Goals"]; + teamName = element["Away Team Name"]; + } + }); + return `Team ${teamName} has the highest goal in a World Cup Finals with the score of ${highestGoal}`; + } + +console.log(getGoals(fifaData)); + + + /* 💪💪💪💪💪 Stretch 3: 💪💪💪💪💪 + Write a function called badDefense() that accepts a parameter `data` and calculates the team with the most goals scored against them per appearance (average goals against) in the World Cup finals */ + + function badDefense(data) { + let highestGoal = 0; + let winnerTeamName = ""; + let loserTeamName = ""; + data.map((element) => { + if (element["Home Team Goals"] > highestGoal) + { + highestGoal = element["Home Team Goals"]; + winnerTeamName = element["Home Team Name"]; + loserTeamName = element["Away Team Name"]; + } + if (element["Away Team Goals"] > highestGoal) + { + highestGoal = element["Away Team Goals"]; + winnerTeamName = element["Away Team Name"]; + loserTeamName = element["Home Team Name"]; + } + }); + return `Team ${loserTeamName} has the has the worst defense against ${winnerTeamName} with a score of ${highestGoal} score against them.`; + } + +console.log(badDefense(fifaData)); \ No newline at end of file