Currying Demo - Problem
Currying is a functional programming technique where a function is transformed into a sequence of functions, each taking a single argument. Instead of taking all arguments at once, a curried function takes the first argument and returns a new function that takes the next argument, and so on.
Your task is to create a curried function that takes a tax rate and returns a function that can calculate tax on any amount.
The curried function should work as follows:
- First call:
createTaxCalculator(taxRate)returns a function - Second call: The returned function takes an
amountand calculates the tax - Formula:
tax = amount * (taxRate / 100)
Return the tax amount rounded to 2 decimal places.
Input & Output
Example 1 — Basic Tax Calculation
$
Input:
taxRate = 8.5, amount = 100
›
Output:
8.5
💡 Note:
Tax calculation: 100 × (8.5 ÷ 100) = 100 × 0.085 = 8.5, rounded to 2 decimal places gives 8.50
Example 2 — Higher Tax Rate
$
Input:
taxRate = 12.75, amount = 250
›
Output:
31.88
💡 Note:
Tax calculation: 250 × (12.75 ÷ 100) = 250 × 0.1275 = 31.875, rounded to 2 decimal places gives 31.88
Example 3 — Small Amount
$
Input:
taxRate = 5.0, amount = 10.50
›
Output:
0.53
💡 Note:
Tax calculation: 10.50 × (5.0 ÷ 100) = 10.50 × 0.05 = 0.525, rounded to 2 decimal places gives 0.53
Constraints
- 0 ≤ taxRate ≤ 100
- 0 ≤ amount ≤ 106
- Result should be rounded to 2 decimal places
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code