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:
Javascript (babel-node)
'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.12console.log(amountAfterTaxes(amount, fedTax)); //27.63console.log(amountAfterTaxes(amount, fedTax, stateTax)); //28.13console.log(amountAfterTaxes(amount, fedTax, stateTax, localTax)); //28.26
Explanation
The problem statement and the sample output require us to do the two main tasks:
- Calculate each tax for the amount given as the first parameter.
- 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 ...