Problems › TypeScript › billing
Total cost of a subscription plan
A subscription charges a monthly rate for a given number of months. Annual plans get one month free.
- Base price equals monthlyAmount multiplied by months.
- If annual is true, subtract one month from the total.
- months of 0 or fewer returns 0.
subscriptionTotal(monthlyAmount: int, months: int, annual: bool) → int
Where you start
function subscriptionTotal(monthlyAmount: number, months: number, annual: boolean): number {
}
Worked examples
| Call | Result |
|---|---|
subscriptionTotal(1000, 12, true) | 11000 |
subscriptionTotal(1000, 12, false) | 12000 |
subscriptionTotal(500, 6, true) | 2500 |
subscriptionTotal(0, 12, false) | 0 |
Hint
Multiply first, then conditionally subtract.
Reference solution in TypeScript
function subscriptionTotal(monthlyAmount: number, months: number, annual: boolean): number {
if (months <= 0) return 0;
let total = monthlyAmount * months;
if (annual) total -= monthlyAmount;
return total;
}