- Invoke functions assigned to a variable.
- Invoke functions stored in a data structure.
- Write functions that return other functions.
- Pass a function to another function.
- Call a function returned by another function.
Functions are a very important part of JavaScript, and you will use them all the time. Without functions, we wouldn't get anything done! In this lab, we'll take a look at how we can use functions as first-class objects to pass them around, store them in variables and data structures, and return them from other functions.
returnFirstTwoDrivers()
— Declare a variable withconst
that is assigned an anonymous function. The assigned function should accept an array of drivers as an argument and return the first two drivers in the array.returnLastTwoDrivers()
— Declare a variable withconst
that is assigned an anonymous function. The assigned function should accept an array of drivers as an argument and return the last two drivers in the array.selectingDrivers
— This is an array containing two elements: the two functions that we previously defined (returnFirstTwoDrivers()
andreturnLastTwoDrivers()
).createFareMultiplier()
— This is a higher-order function that takes in one argument, an integer, and returns a function that will multiply a fare for a ride accordingly. IfcreateFareMultiplier()
receives an argument of4
, it will return a function that takes in a fare as an argument and quadruples the fare.fareDoubler()
— Declare a variable withconst
and assign a function returned bycreateFareMultiplier()
to it. InvokecreateFareMultiplier()
in such a way that the newfareDoubler()
function accepts a fare as its lone argument and doubles it.fareTripler()
— Declare a variable withconst
and assign a function returned bycreateFareMultiplier()
to it. InvokecreateFareMultiplier()
in such a way that the newfareTripler()
function accepts a fare as its lone argument and triples it.selectDifferentDrivers()
— This function takes two arguments, an array ofdrivers
and either thereturnFirstTwoDrivers()
orreturnLastTwoDrivers()
function. Based on these two arguments,selectDifferentDrivers()
will return either the first two drivers or the last two drivers.
View First-Class Functions Lab on Learn.co and start learning to code for free.