# 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 November 14, 2026

By Gregor Dzierzon

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

Open this lesson's GitHub repository, [github.com/ask-codey/exercise-javascript-bike-rental-prices](https://github.com/ask-codey/exercise-javascript-bike-rental-prices), also linked from the **Exercise** box beside the lesson. Clone it wherever you keep your practice work. In a terminal, in that folder:

```bash
git clone https://github.com/ask-codey/exercise-javascript-bike-rental-prices.git
```

Git makes a folder, `exercise-javascript-bike-rental-prices`, with the exercise code inside.

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 use `import` and `export`.

## 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:

```javascript
/*
 * 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.

1. **Finish `isWeekend`.** It gives back `true` for Saturday or Sunday.
2. **Finish `rentalPrice(hours, isWeekendDay, isMember)`.** Pick the hourly rate, take off the member discount, multiply, and apply the day cap.
3. **Finish `bikeSize(height)`.** Under 54 inches is `"kids"`, under 63 `"small"`, under 69 `"medium"`, and taller is `"large"`.
4. **Finish `groupReceipt(riders, hours, isWeekendDay)`.** One line per bike, then the total. Every fourth bike is free, and a one-hour ride says `1 hour`, not `1 hours`.

## Check your work

In VS Code, open a terminal in the folder (**Terminal > New Terminal**) and run the tests:

```bash
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.** `groupReceipt` needs each bike's price, and `rentalPrice` already 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:

```text
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.

## Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. You don't need Git for this: you upload your files in the browser, as the lesson *Put Your Code on GitHub* (Module 1) shows screen by screen.

1. On GitHub, click **+**, then **New repository**. (No account yet? Sign up at github.com: it's free.) Name it (for example `exercise-javascript-bike-rental-prices`), choose **Public**, turn **Add README** on so the upload button appears, and click **Create repository**. Your folder's own README replaces GitHub's when you upload it.
2. Choose **Add file**, then **Upload files**. In your exercise folder, select everything (Ctrl+A, or Cmd+A on a Mac) and drag it onto the upload area. Drag what's inside the folder, not the folder itself, so `package.json` lands at the top of your repo.
3. Under **Commit changes**, type a short note, such as *My solution*, and choose **Commit changes**.
4. Paste your repo's address, such as `https://github.com/your-name/exercise-javascript-bike-rental-prices`, into **Submit your exercise** on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Upload the changed files again (each one replaces the file with the same name), commit, and submit again: your latest submission is the one that counts.

## 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.

## Key terms

- [if Statement](https://www.ask-codey.com/glossary/if-statement-javascript)
- [Logical Operators](https://www.ask-codey.com/glossary/logical-operators-javascript)
- [Ternary Operator](https://www.ask-codey.com/glossary/ternary-operator-javascript-2)
- [for Loop](https://www.ask-codey.com/glossary/for-loop-javascript)
- [continue](https://www.ask-codey.com/glossary/continue-javascript)

## Related

- [Making Decisions with if and else](https://www.ask-codey.com/lessons/javascript-conditions-if-and-else)
- [Combining Conditions with And, Or and Not](https://www.ask-codey.com/lessons/javascript-conditions-and-or-not)
- [switch and the Conditional Operator](https://www.ask-codey.com/lessons/javascript-conditions-switch-and-ternary)
- [Counting with for](https://www.ask-codey.com/lessons/javascript-loops-for)

Source: https://www.ask-codey.com/lessons/exercise-javascript-bike-rental-prices
