Problems › TypeScript › pricing
Apply a promo percentage
The till applies a promo code to a price held in minor units — kuruş, cents, whatever the currency splits into.
- A percentage outside 1 to 100 is a bad code: return the price untouched.
- Stay in integers and drop the remainder, so 10% off 999 is 900, not 899.1.
discountedPrice(price: int, percent: int) → int
Where you start
function discountedPrice(price: number, percent: number): number {
}
Worked examples
| Call | Result |
|---|---|
discountedPrice(1000, 20) | 800 |
discountedPrice(999, 10) | 900 |
discountedPrice(1000, 0) | 1000 |
discountedPrice(1000, 150) | 1000 |
Hint
Reject the bad range first, then integer-divide.
Reference solution in TypeScript
function discountedPrice(price: number, percent: number): number {
if (percent < 1 || percent > 100) return price;
return price - Math.floor((price * percent) / 100);
}