convert this Python function to TypeSCript def jpr\service\fee(prod\value): progressive…
- User
convert this Python function to TypeSCript
def jpr_service_fee(prod_value):
progressive rates
thresholds = [0,80,128,208,336,544,880] rates = [0.0990,0.0690,0.0480,0.0340,0.0240,0.0170,0.012] fees = 0 for i in range(len(rates)): if i == len(thresholds) - 1 or prod_value < thresholds[i+1]: fees += (prod_value - thresholds[i]) * rates[i] break else: fees += (thresholds[i+1] - thresholds[i]) * rates[i] return fees
- Assistant
Here is the equivalent TypeScript function:
codefunction jprServiceFee(prodValue: number): number { // progressive rates const thresholds = [0, 80, 128, 208, 336, 544, 880]; const rates = [0.0990, 0.0690, 0.0480, 0.0340, 0.0240, 0.0170, 0.012]; let fees = 0; for (let i = 0; i < rates.length; i++) { if (i === thresholds.length - 1 || prodValue < thresholds[i + 1]) { fees += (prodValue - thresholds[i]) * rates[i]; break; } else { fees += (thresholds[i + 1] - thresholds[i]) * rates[i]; } } return fees; }Note that in TypeScript, function names should be camelCase and the first letter of each word should be capitalized (e.g. "jprServiceFee" instead of "jpr_service_fee"). Also, the function parameters and variables should be in camelCase.
- User
write 5 unit tests for this function
- Assistant
Here are five unit tests that you could write for the
jprServiceFeefunction:- Test that the function returns the correct fee for a product value of 0:
codeit("should return 0 for a product value of 0", () => { expect(jprServiceFee(0)).toEqual(0); });- Test that the function returns the correct fee for a product value within the first threshold range:
codeit("should return the correct fee for a product value within the first threshold range", () => { expect(jprServiceFee(50)).toEqual(4.95); });- Test that the function returns the correct fee for a product value within a threshold range other than the first one: