- Use closures to customize functions
- Use closures as a mechanism of encapsulations
In the lab that follows, practice using closures to construct functions that have reference to variables that the execution scope does not.
Create the following functions:
-
produceDrivingRange()
- Calculates whether a given trip is within range. For example,produceDrivingRange(10)
returns a function that will take two strings as arguments and returns a message stating whether the trip is in range. Iffoo = produceDrivingRange(10)
, thenfoo('12th', '15th')
would return"within range by 7"
andfoo('12th', '30th')
would return"8 blocks out of range"
. We recommend referencing thetest/indexTest.js
for more details. -
produceTipCalculator()
- Returns a function that then calculates a tip. For example,produceTipCalculator(.10)
returns a function that calculates ten percent tip on a fare.produceTipCalculator(.20)
returns a function that calculates twenty percent tip on a fare. -
createDriver
is a function that returns aDriver
class. The class has reference to adriverId
that is incremented each time a new driver is created. The rest of the code base does not have access todriverId
.
The functions should make use of closures to achieve the specified goals.
View Advanced Scope Closures Lab on Learn.co and start learning to code for free.