Problems › JavaScript › billing
Early payment discount
A vendor rewards early payment: the further ahead the payment, the larger the discount off the subtotal.
- Days early >= 10 earns a 2% discount, rounded down.
- Days early >= 5 (but under 10) earns a 1% discount, rounded down.
- Under 5 days earns no discount.
- The discount is floor(subtotal × percentage / 100).
earlyDiscount(subtotal: int, daysEarly: int) → int
Where you start
function earlyDiscount(subtotal, daysEarly) {
}
Worked examples
| Call | Result |
|---|---|
earlyDiscount(10000, 10) | 200 |
earlyDiscount(5000, 5) | 50 |
earlyDiscount(7500, 15) | 150 |
earlyDiscount(999, 3) | 0 |
Hint
Check the thresholds from largest to smallest.
Reference solution in JavaScript
function earlyDiscount(subtotal, daysEarly) {
if (daysEarly >= 10) return Math.floor(subtotal * 2 / 100);
if (daysEarly >= 5) return Math.floor(subtotal / 100);
return 0;
}