Search⌘ K
AI Features

Solution Review: Calculate the Tax

Explore how to solve tax calculation problems by creating functions that compute individual taxes and sum them with the original amount. Understand using array map and reduce methods to process tax data efficiently, with a focus on precision and correct application of JavaScript functional techniques.

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.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 two functions inside the ...