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 amount and 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
INPUTALGORITHMRESULTTax Rate8.5%Amount$1001Create Tax Calculator2Store Rate in Closure3Return Specialized Function4Apply to AmountTax Calculated$8.50100 × 0.085Key Insight:Currying transforms multi-parameter functions into reusable, specialized toolsTutorialsPoint - Currying Demo | Functional Programming
Asked in
Google 25 Microsoft 20 Facebook 15
27.2K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen