Writing Small Functions as Arrows

Your describeDock() now takes bike and free-slot counts. The crew lead also wants each dock’s fill rate. Keep that formula in a helper so every report line uses the same calculation. JavaScript’s arrow syntax suits this small function.

Write the Fill Rate as an Arrow

The calculations here assume non-negative bike and free-slot counts with a positive combined capacity.

In Python, you’d use def for a named helper. Python’s lambda and JavaScript’s arrow function, a function written with =>, offer these expression forms:

Python JavaScript
lambda bikes, slots: bikes / (bikes + slots) * 100 (bikes, slots) => bikes / (bikes + slots) * 100
fill_rate(14, 1) fillRate(14, 1)

The arrow needs no keyword. Its parameters precede =>, and an expression body returns its value automatically. Store the function in a variable declared with const and call it normally. Replace ~/dock-report/dock-report.js with:

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.