Problems › TypeScript › billing
Calculate tax on a shipping charge
Some regions tax shipping, others do not. Compute the tax amount on a shipping cost.
- If taxable, return floor(shippingCost × taxPercent / 100).
- If not taxable, return 0.
taxOnShipping(shippingCost: int, taxPercent: int, taxable: bool) → int
Where you start
function taxOnShipping(shippingCost: number, taxPercent: number, taxable: boolean): number {
}
Worked examples
| Call | Result |
|---|---|
taxOnShipping(8000, 10, true) | 800 |
taxOnShipping(8000, 10, false) | 0 |
taxOnShipping(0, 20, true) | 0 |
taxOnShipping(5500, 8, true) | 440 |
Hint
One conditional and an integer division.
Reference solution in TypeScript
function taxOnShipping(shippingCost: number, taxPercent: number, taxable: boolean): number {
if (!taxable) return 0;
return Math.floor(shippingCost * taxPercent / 100);
}