JSFiddle - React, Tailwind, and code Playground

by Frank Liu

JavaScript

const resolvePriceNumberString = (number) => parseFloat(number).toFixed(2);
const resolveTotal = (totalAmount) => resolvePriceNumberString(totalAmount);
const resolveVAT = (countryCode) => (countryCode === "GB" ? 0.2 : 0);

const resolveTax = ({ totalAmount, countryCode }) => {
  const vat = resolveVAT(countryCode);
  const total = resolveTotal(totalAmount);
  if (0 <= vat && vat < 1) {
    return resolvePriceNumberString(total * vat);
  } else {
    return 0;
  }
};

const resolvePriceWithoutVAT = ({ totalAmount, countryCode }) => {
  const total = resolveTotal(totalAmount);
  const tax = resolveTax({ totalAmount:total, countryCode });
  console.log({total, tax})
  return resolvePriceNumberString(total - tax);
};

console.log(resolvePriceWithoutVAT({totalAmount :100, countryCode : 'GB'}))