Problems › TypeScript › logistics
What does this parcel cost to send
A courier prices every parcel as: the first five kilograms cost 300 (minor units) and each further kilogram adds 60.
- A parcel at or under five kilograms always costs 300.
- Every whole kilogram over five adds 60; there are no fractions here.
- A parcel cannot weigh negative kilograms: return 0 for bad input.
parcelCost(weight: int) → int
Where you start
function parcelCost(weight: number): number {
}
Worked examples
| Call | Result |
|---|---|
parcelCost(3) | 300 |
parcelCost(5) | 300 |
parcelCost(6) | 360 |
parcelCost(10) | 600 |
Hint
Subtract the free five, multiply what is left, and add the base.
Reference solution in TypeScript
function parcelCost(weight: number): number {
if (weight < 0) return 0;
return 300 + Math.max(0, weight - 5) * 60;
}