Calculate Tax Amount

Easy
4
1
27.0% Acceptance

In this lab, you have to implement a function that calculates the tax amount based on given progressive tax brackets. The given tax brackets have an upper bound and a tax rate at that bracket.

You will be given a 2D integer array brackets where brackets[i] = [upperi, percenti] represents that the ith tax bracket has an upper-bound income of upperi and is taxed at a rate of percenti.

The tax brackets are sorted by their upper bound, meaning upperi-1 < upperi for 0 < i < brackets.length.

Based on the given brackets and the income, you need to calculate the total tax to be paid.

Here's an example to help you understand the problem better:

Example:

// brackets = [[3, 50], [7, 10], [12, 25]] // income = 10 // Output: 2.65 // Explanation: // Based on your income, you have: // - 3 dollars in the 1st tax bracket (tax rate 50%) // - 4 dollars in the 2nd tax bracket (tax rate 10%) // - 3 dollars in the 3rd tax bracket (tax rate 25%) // In total, you pay $3 * 50% + $4 * 10% + $3 * 25% = $2.65 in taxes.

Function Signature:

/** * @param {number[][]} brackets * @param {number} income * @return {number} */ const calculateTax = (brackets, income) => { // Your code here };

Constraints:

  • 1 <= brackets.length <= 100
  • 1 <= upperi <= 1000
  • 0 <= percenti <= 100
  • 0 <= income <= 1000
  • upperi is sorted in ascending order.
  • All the values of upperi are unique.
  • The upper bound of the last tax bracket is greater than or equal to income.

Challenges

  1. Implement the calculateTax function.
  2. Export the calculateTax function.
  3. Make sure you're default export the function