...

/

Solution Review: Calculate the Tax

Solution Review: Calculate the Tax

Learn the solution to calculate the tax problem in detail.

We'll cover the following...

Solution

We can solve this problem as follows:

Press + to interact
'use strict';
const amountAfterTaxes = function(amount, ...taxes) {
const computeTaxForAmount = function(tax) {
return amount * tax / 100.0;
};
const totalValues = function(total, value) {
return total + value;
};
return taxes.map(computeTaxForAmount).reduce(totalValues, amount).toFixed(2);
};
const amount = 25.12;
const fedTax = 10;
const stateTax = 2;
const localTax = 0.5;
console.log(amountAfterTaxes(amount)); //25.12
console.log(amountAfterTaxes(amount, fedTax)); //27.63
console.log(amountAfterTaxes(amount, fedTax, stateTax)); //28.13
console.log(amountAfterTaxes(amount, fedTax, stateTax, localTax)); //28.26

Explanation

The problem statement and the sample output require us to do the two main tasks:

  1. Calculate each tax for the amount given as the first parameter.
  2. Add all the taxes in the amount to return the total amount after the application of taxes.

Now for the implementation of these tasks, one easy way is to make ...

Access this course and 1400+ top-rated courses and projects.