Drill

ProblemsTypeScript › billing

Payment method surcharge

easybillingTypeScript

Certain payment methods carry a surcharge. Look up the percentage and apply it to the subtotal.

surchargeByMethod(surchargeRates: map<string, int>, method: string, subtotal: int) → int

Solve it in the editor →

Where you start

function surchargeByMethod(surchargeRates: Record<string, number>, method: string, subtotal: number): number {
  
}

Worked examples

CallResult
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 TypeScript
function surchargeByMethod(surchargeRates: Record<string, number>, method: string, subtotal: number): number {
  let rate = surchargeRates[method] ?? 0;
  return Math.floor(subtotal * rate / 10000);
}

The same problem in another language

More billing problems in TypeScript