-1

I'm working on a tax calculator app with JavaScript and have a problem adding let values.

When trying to add 3 different tax values together, in the console, the answer I always get is the first of the values.

let basicRate,
  higherRate,
  additionalRate;

function calculateTaxDue(grossSalary) {
  if (grossSalary > 150000) {
    basicRate = parseFloat((46351 - 11000) * 0.2).toFixed(2);
    higherRate = parseFloat((150000 - 46351) * 0.4).toFixed(2);
    additionalRate = parseFloat((grossSalary - 150000) *
      0.45).toFixed(2)
    taxDue = parseFloat((basicRate + higherRate +
      additionalRate)).toFixed(2);
  }
}


calculateTaxDue(150001)

console.log(parseFloat(basicRate).toFixed(2));
console.log(parseFloat(higherRate).toFixed(2));
console.log(parseFloat(additionalRate).toFixed(2));

console.log(parseFloat(basicRate + higherRate +
  additionalRate).toFixed(2));

Just prints the first value (basicRate) to the console. I'm confused by this.

Apologies for lack of detail first time around.

Thanks

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Andy83
  • 79
  • 9
  • 5
    The code as it is would print `NaN`. Please provide a [mcve]. Also, adding values together doesn't have anything to do with how the variables have been declared (`let`, `var` or `const`). There is no such thing as *"let values"*. If you only "get the first value" then the other two values may be `0`. – Felix Kling May 08 '18 at 17:52
  • 2
    `undefined + undefined + undefined` is `NaN`. What good is a demo that doesn't demonstrate your problem? –  May 08 '18 at 17:52
  • 1
    Looks to me like they all print. –  May 08 '18 at 18:18
  • 1
    You are getting this result because `basicRate`, `higherRate` and `additionalRate` are **strings**. `basicRate + higherRate + additionalRate` produces the string `"7070.2041459.600.45"` and `parseFloat("7070.2041459.600.45").toFixed(2)` the returns `7070.20`. Only use `.toFixed` when you actually want to **display** numbers. – Felix Kling May 08 '18 at 18:19
  • Aha, brilliant - thank you Felix! – Andy83 May 08 '18 at 18:20

1 Answers1

2

You are getting this result because basicRate, higherRate and additionalRate are strings.

basicRate + higherRate + additionalRate produces the string "7070.2041459.600.45" and parseFloat("7070.2041459.600.45").toFixed(2) the returns 7070.20.

Only use .toFixed when you actually want to display numbers:

let basicRate,
  higherRate,
  additionalRate;

function calculateTaxDue(grossSalary) {
  if (grossSalary > 150000) {
    basicRate = (46351 - 11000) * 0.2;
    higherRate = (150000 - 46351) * 0.4;
    additionalRate = (grossSalary - 150000) *
      0.45;
    taxDue = basicRate + higherRate +
      additionalRate;
  }
}


calculateTaxDue(150001)

console.log(basicRate.toFixed(2));
console.log(higherRate.toFixed(2));
console.log(additionalRate.toFixed(2));

console.log((basicRate + higherRate +
  additionalRate).toFixed(2));

Most importantly: Read the documentation of the functions you are using.

  • parseFloat expects a string as input and returns a number. There is no point in passing a number to it like you do in parseFloat((46351 - 11000) * 0.2).
  • .toFixed returns a string. Don't use it if you want to actually perform further computations with the number values.

And finally, don't use floating point values to perform monetary computations. The results will be incorrect due to rounding errors and loss of precision. Express all numbers as integers.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143