Problems › TypeScript › billing
Compute the final charge on an order
Combine a subtotal, an optional discount, and shipping to produce the final amount due.
- Subtract the discount from the subtotal, clamped at zero.
- Then add the shipping cost.
finalCharge(subtotal: int, discount: int, shipping: int) → int
Where you start
function finalCharge(subtotal: number, discount: number, shipping: number): number {
}
Worked examples
| Call | Result |
|---|---|
finalCharge(5000, 500, 800) | 5300 |
finalCharge(1000, 2000, 500) | 500 |
finalCharge(3000, 0, 1000) | 4000 |
finalCharge(0, 0, 0) | 0 |
Hint
Max(0, subtotal − discount) then add shipping.
Reference solution in TypeScript
function finalCharge(subtotal: number, discount: number, shipping: number): number {
const net = subtotal > discount ? subtotal - discount : 0;
return net + shipping;
}