Coding Challenge: Higher-Order Function
Let's use higher-order functions and currying in this challenge.
We'll cover the following
Problem statement
The buyTicket
function below calculates the price of a ticket and saves the request in our mock database. The price logic is currently hardcoded, but we’d like to change it because in the future, we might prefer to ask wealthy customers to pay more. Our task is to write a calculatePrice
function that performs the calculation that’s now hardcoded in the buyTicket
function. It takes customer age
as a parameter. Next, create a buyTicketBase
function that takes one parameter called calculate
(which is a function that takes a number and returns a number) and returns a function that takes two parameters, show
and customer
. It uses the calculate
function that we passed in to retrieve the price and saves that info in the database as before. Finally, create newBuyTicket
by passing in the calculatePrice
function as a parameter to the buyTicketBase
function.
Get hands-on with 1400+ tech skills courses.