Reputation: 7
For this one I have built several functions and a higher order function. I'm looking to pass the three smaller functions as a parameter to the higher order function. I'm getting an error that callback is not a function when I try to invoke the higher order function and pass as a parameter. Any ideas?
{
"Year": 2002,
"Datetime": "02 Jun 2002 - 18:30",
"Stage": "Group F",
"Stadium": "Saitama Stadium 2002",
"City": "Saitama",
"Home Team Name": "England",
"Home Team Goals": 1,
"Away Team Goals": 1,
"Away Team Name": "Sweden",
"Win conditions": "",
"Attendance": 52721,
"Half-time Home Goals": 1,
"Half-time Away Goals": 0,
"Referee": "SIMON Carlos (BRA)",
"Assistant 1": "OLIVEIRA Jorge (BRA)",
"Assistant 2": "DUPANOV Yuri (BLR)",
"RoundID": 43950100,
"MatchID": 43950005,
"Home Team Initials": "ENG",
"Away Team Initials": "SWE"
},
{
"Year": 2002,
"Datetime": "02 Jun 2002 - 20:30",
"Stage": "Group B",
"Stadium": "Gwangju World Cup Stadium",
"City": "Gwangju",
"Home Team Name": "Spain",
"Home Team Goals": 3,
"Away Team Goals": 1,
"Away Team Name": "Slovenia",
"Win conditions": "",
"Attendance": 28598,
"Half-time Home Goals": 1,
"Half-time Away Goals": 0,
"Referee": "GUEZZAZ Mohammed (MAR)",
"Assistant 1": "TOMUSANGE Ali (UGA)",
"Assistant 2": "BEREUTER Egon (AUT)",
"RoundID": 43950100,
"MatchID": 43950008,
"Home Team Initials": "ESP",
"Away Team Initials": "SVN"
},
]
function getFinals(arr)
{
return arr.filter(team => team.Stage === "Final");
}
//console.log(getFinals(fifaData));
function getYears(arr, callback)
{
const years = callback(arr).map(team => team.Year);
return years;
}
function getWinners(arr, func)
{
const winners = [];
const winIt = func(arr);
console.log(winIt);
for (let i = 0; i < winIt.length; i++)
{
if(winIt[i]['Home Team Goals'] > winIt[i]['Away Team Goals'])
{
winners.push(winIt[i]['Home Team Name']);
}
else
{
winners.push(winIt[i]['Away Team Name']);
}
}
return winners
}
function getWinnersByYear(arr, cbThree, cbFour,cbFive)
{
const years = cbThree(arr, cbFive);
const winners = cbFour(arr, cbThree);
const full = [];
for(let j = 0; j < years.length; j++)
{
full.push(`In ${years[j]}, ${winners[j]} won the world cup!`);
}
return full;
}
console.log(getWinnersByYear(fifaData, getYears, getWinners, getFinals));
Upvotes: 1
Views: 53
Reputation: 3231
You're calling getYears
with only one argument in getWinners
:
function getWinners(arr, func) // <- getYears was passed as `func`
{
const winners = [];
const winIt = func(arr); // <- here
In JS, omitting an argument in a function call is equivalent to providing undefined
for that parameter.
So in this call, the parameter callback
is assigned undefined
, which is indeed not a function (and thus can't be invoked).
I would recommend giving meaningful names to your parameters. It improves the readability and helps track down this kind of errors.
The general flow could be simplified too, making less heavy use of callbacks -- for instance, in getYears
: consider how passing directly the result of callback(arr)
instead of arr
and callback
would be simpler and equivalent.
Upvotes: 1