Closures Lab
Objectives
Use closures to customize functions
Use closures as a mechanism of encapsulations
Instructions
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.jsfor 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.createDriveris a function that returns aDriverclass. The class has reference to adriverIdthat 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.
Clone : https://github.com/learn-co-curriculum/js-advanced-scope-closures-lab
Last updated