Building Functions From Functions

With the class reminder ported, the manager has a new job. On Wednesday, student night, every bill gets 20 percent off. The Python script builds the student night prices from a factory, functools.partial(), and functools.reduce(). In JavaScript, each becomes an arrow, with nothing to import, and you’ll port them one at a time, starting with the factory.

Make a Discount With an Arrow That Returns an Arrow

Python’s make_discount(rate) returns a lambda. The JavaScript port fits on one line:

Language: JavaScript
const makeDiscount = (rate) => (amount) => amount * (1 - rate);

const studentNight = makeDiscount(0.2);
console.log(studentNight(18));
console.log(`£${studentNight(18).toFixed(2)}`);

An adult with shoes pays what the Python script charges:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.