Problems › JavaScript › billing
Payment method surcharge
Certain payment methods carry a surcharge. Look up the percentage and apply it to the subtotal.
- Rates in the map are basis points — hundredths of a percent.
- The surcharge is floor(subtotal × rate / 10000).
- An unknown method means no surcharge.
surchargeByMethod(surchargeRates: map<string, int>, method: string, subtotal: int) → int
Where you start
function surchargeByMethod(surchargeRates, method, subtotal) {
}
Worked examples
| Call | Result |
|---|---|
surchargeByMethod({"visa":150,"amex":300}, "amex", 10000) | 300 |
surchargeByMethod({"visa":150}, "paypal", 5000) | 0 |
surchargeByMethod({"visa":150}, "visa", 8000) | 120 |
surchargeByMethod({}, "visa", 1000) | 0 |
Hint
Map lookup, then a single integer division by ten thousand.
Reference solution in JavaScript
function surchargeByMethod(surchargeRates, method, subtotal) {
let rate = surchargeRates[method] ?? 0;
return Math.floor(subtotal * rate / 10000);
}