---
id: 6a15cadf5f240d05a2649557
title: "Challenge 313: Rental Cost"
challengeType: 28
dashedName: challenge-313
---

# --description--

Given a rental timestamp, a return timestamp, and a rental tier, return the total cost of the rental including any late fees.

- Given timestamps are UTC ISO strings, for example: `"2026-06-18T18:30:00Z"`.
- The rental tier is the number of days before the rental is due back: `1`, `3`, or `7`.
- Rentals are due back by 12:00 PM UTC or earlier on the last day of the rental period. For example, a 1-day rental checked out at any time on March 15 is due back by 12:00 PM UTC on March 16.
- Each day past the due date and time incurs a late fee.

Pricing is as follows:

| Tier | Base cost | Late fee per day |
| - | - | - |
| 1 day | $4.99 | $3.99 |
| 3 days | $3.99 | $2.99 |
| 7 days | $2.99 | $0.99 |

Return the total cost rounded to two decimal places in the format `"$D.CC"`.

# --hints--

`getRentalCost("2026-06-18T18:30:00Z", "2026-06-19T10:30:00Z", 1)` should return `"$4.99"`.

```js
assert.equal(getRentalCost("2026-06-18T18:30:00Z", "2026-06-19T10:30:00Z", 1), "$4.99");
```

`getRentalCost("2026-06-18T14:30:00Z", "2026-06-20T12:30:00Z", 1)` should return `"$12.97"`.

```js
assert.equal(getRentalCost("2026-06-18T14:30:00Z", "2026-06-20T12:30:00Z", 1), "$12.97");
```

`getRentalCost("2026-06-18T10:15:00Z", "2026-06-18T19:45:00Z", 3)` should return `"$3.99"`.

```js
assert.equal(getRentalCost("2026-06-18T10:15:00Z", "2026-06-18T19:45:00Z", 3), "$3.99");
```

`getRentalCost("2026-06-18T15:20:00Z", "2026-06-23T08:10:00Z", 3)` should return `"$9.97"`.

```js
assert.equal(getRentalCost("2026-06-18T15:20:00Z", "2026-06-23T08:10:00Z", 3), "$9.97");
```

`getRentalCost("2026-06-18T12:00:00Z", "2026-06-25T12:00:00Z", 7)` should return `"$2.99"`.

```js
assert.equal(getRentalCost("2026-06-18T12:00:00Z", "2026-06-25T12:00:00Z", 7), "$2.99");
```

`getRentalCost("2026-06-18T08:00:00Z", "2027-06-18T14:00:00Z", 7)` should return `"$358.40"`.

```js
assert.equal(getRentalCost("2026-06-18T08:00:00Z", "2027-06-18T14:00:00Z", 7), "$358.40");
```

# --seed--

## --seed-contents--

```js
function getRentalCost(rented, returned, tier) {

  return rented;
}
```

# --solutions--

```js
function getRentalCost(rented, returned, tier) {
  const pricing = {
    1: { base: 4.99, late: 3.99 },
    3: { base: 3.99, late: 2.99 },
    7: { base: 2.99, late: 0.99 },
  };

  const rentedDate = new Date(rented);
  const returnDate = new Date(returned);
  const due = new Date(rentedDate);
  due.setUTCDate(due.getUTCDate() + tier);
  due.setUTCHours(12, 0, 0, 0);
  const { base, late } = pricing[tier];

  if (returnDate <= due) {
    return `$${base.toFixed(2)}`;
  }

  const msPerDay = 1000 * 60 * 60 * 24;
  const daysLate = Math.ceil((returnDate - due) / msPerDay);
  const total = base + daysLate * late;

  return `$${total.toFixed(2)}`;
}
```
