Exercise: Bike Rental Prices
Finish four functions for a bike rental shop, using conditions, the conditional operator and a for loop, and let the tests check every price, bike size and group receipt.
Apprentice · JavaScript · Updated
Hilltop Bikes works out every price by hand, on a notepad at the counter: is it the weekend, is the rider a member, did the rental hit the day cap? On a busy Saturday, with a family of five waiting, mistakes happen. In this exercise you write the code that does it for them: four small functions, with tests that check every one as you go.
Get the exercise code
In the Exercise box beside the lesson, choose Get the starter on GitHub to open its GitHub repository. Click Code, then Download ZIP, and unzip it wherever you keep your practice work. Then open the folder in VS Code.
The folder holds:
rentals.js: the four functions for you to finish.app.js: prints your functions' answers for a few customers, so you can see them at work.rentals.test.js: the tests that check your functions. You don't need to change these.package.json: tells Node that these files useimportandexport.
What to do
Each function in rentals.js is already written except for its body. The values in its parentheses are its inputs: variables that already hold a value when the function runs. return hands back the answer and ends the function on that line. The next chapter, Functions, shows how to write functions of your own; here you only fill them in.
The comment above each function says what it should give back, with examples:
/*
* Give back true when the day is "Saturday" or "Sunday", and false for every other day.
* Days always start with a capital letter.
*
* isWeekend("Saturday") -> true
* isWeekend("Sunday") -> true
* isWeekend("Monday") -> false
*/
export function isWeekend(day) {
return false;
}
The shop's prices are at the top of the file: $12 an hour on weekdays and $15 on weekends, members pay $2 less an hour, and a rental never costs more than the day cap of $60 on a weekday or $75 on a weekend.
- Finish
isWeekend. It gives backtruefor Saturday or Sunday. - Finish
rentalPrice(hours, isWeekendDay, isMember). Pick the hourly rate, take off the member discount, multiply, and apply the day cap. - Finish
bikeSize(height). Under 54 inches is"kids", under 63"small", under 69"medium", and taller is"large". - Finish
groupReceipt(riders, hours, isWeekendDay). One line per bike, then the total. Every fourth bike is free, and a one-hour ride says1 hour, not1 hours.
Check your work
In VS Code, open a terminal in the folder (Terminal > New Terminal) and run the tests:
node --test
At first every test fails. That's expected: each function you finish turns its tests green. When a test fails, its message says which values it used and what it expected, for example 6 hours on a weekday is 72 by the hour, so it should cost the cap: 60.
To see your functions' answers, run node app.js.
Hints
- Work out an example by hand first. For 7 hours on a weekday, for a member: what's the hourly rate, the price before the cap, and the cap?
- One condition, two values. The weekend decides both the hourly rate and the day cap. The conditional operator from switch and the Conditional Operator picks a value in one line.
- Order matters. In
bikeSize, check the smallest size first, as in Making Decisions with if and else. - Use what you've built.
groupReceiptneeds each bike's price, andrentalPricealready works it out. - Which bikes are free?
%gives the remainder of a division. Bike 4 and bike 8 have a remainder of 0 when you divide them by 4.continue, from Counting with for, can skip the rest of a pass.
When you're done
Every test is green, and node app.js prints:
Is Saturday a weekend day? true
Is Tuesday a weekend day? false
3 hours on a weekday: $36
3 hours on a weekend, for a member: $39
8 hours on a weekend: $75
A rider 66 inches tall rides a medium bike.
A group of 5, riding 2 hours on a weekday:
Bike 1: 2 hours, $24
Bike 2: 2 hours, $24
Bike 3: 2 hours, $24
Bike 4: 2 hours, free
Bike 5: 2 hours, $24
Total: $96
Now say Hilltop Bikes raises its weekend rate to $16. How many lines of your code do you need to change? If the answer is more than one, look for a 15 that appears in two places.
So what
Every price on a checkout page, from bike rentals to plane tickets, is a handful of rules like these: conditions for the special cases, and a loop for each item. You've written the real thing.