0

I recently wrote a Python tax calculator for my mother, who is an accountant. It seems to work just fine, but she hates using the terminal, so I decided to re-write it in JavaScript which I am currently learning at university. Here is the Python calculator:

print("Welcome to the tax calculator!")

assessible_income = int(input("Enter your assessible income in AUD: "))
allowable_deductions = int(input("Enter your allowable deductions in AUD: "))

taxable_income = assessible_income - allowable_deductions

tax_bracket_0 = range(0, 18201)
tax_bracket_1 = range(18201, 45001)
tax_bracket_2 = range(45001, 120001)
tax_bracket_3 = range(120001, 180001)
tax_bracket_4 = range(180001, 1000000)

if (taxable_income in tax_bracket_0):
    print("You do not pay any tax.")
elif (taxable_income in tax_bracket_1):
    print("You will pay $0 tax on the first $18200 of your income, and 19 cents for each dollar over $18200.")
    tax = ((taxable_income - 18200)*0.19)
    medicare_levy = taxable_income*0.02
    total_tax = tax + medicare_levy
    print("You will pay", total_tax, "dollars in tax, including Medicare levy.")
    print("Your Medicare levy is, in AUD: ", medicare_levy)
elif (taxable_income in tax_bracket_2):
    print("You will pay $5092 plus $0.325 for each $1 over $45000.")
    tax1 = ((5092) + ((taxable_income - 45000)*0.325))
    medicare_levy1 =  taxable_income*0.02
    total_tax1 = tax1 + medicare_levy1
    print("You will pay", total_tax1, "dollars in tax, including Medicare levy.")
    print("Your Medicare levy is, in AUD: ", medicare_levy1)
elif (taxable_income in tax_bracket_3):
    print("You will pay $29467 plus $0.37 for every dollar over 120000")
    tax2 = ((29467) + ((taxable_income - 120000)*0.37))
    medicare_levy2 = taxable_income*0.02
    total_tax2 = tax2 + medicare_levy2
    print("You will pay", total_tax2, "dollars in tax, including Medicare levy.")
    print("Your Medicare levy is, in AUD: ", medicare_levy2)
elif (taxable_income in tax_bracket_4):
    print("You will pay $51667 plus $0.45 for each dollar over $180000.")
    tax3 = ((51667) + ((taxable_income - 180000)*0.45))
    medicare_levy3 = taxable_income*0.02
    total_tax3 = tax3 + medicare_levy3
    print("You will pay", total_tax3, "dollars in tax, including Medicare levy.")
    print("Your Medicare levy is, in AUD: ", medicare_levy3)
else:
    print("You have too much money for this calculator. Good luck.")

The JavaScript calculator does not function like the Python one does, though. It appears to work for income values of up to 75000 with 0 deductions, but after that, it appears to calculate to the wrong tax bracket. Running the two scripts side-by-side should highlight the difference. Here is the JS:

function range(start, end) {
  const ans = [];
  for (let i = start; i <= end; i++) {
    ans.push(i);
  }
  return ans;
}

const taxBracketZero = range(1, 18201);
const taxBracketOne = range(18201, 45001);
const taxBracketTwo = range(45001, 120001);
const taxBracketThree = range(120001, 180001);
const taxBracketFour = range(180001, 1000000)

function calculateTax() {
  let income = document.getElementById("income").value;
  let deductions = document.getElementById("deductions").value;
  let tax = income - deductions

  if (tax in taxBracketZero) {
    document.getElementById("result").innerHTML = "You do not pay any tax.";
  } else if (tax in taxBracketOne) {
    document.getElementById("result").innerHTML = "You will pay $0 tax on the first $18200 of your income, and 19 cents for each dollar over $18200.";
    let taxable_income = ((tax - 18200) * 0.19);
    let medicare_levy = tax * 0.02;
    let total_tax = taxable_income + medicare_levy;
    document.getElementById("result1").innerHTML = "You will pay " + total_tax + " dollars in tax, including Medicare levy.";
    document.getElementById("result2").innerHTML = "Your Medicare levy is, in AUD: " + medicare_levy;
  } else if (tax in taxBracketTwo) {
    document.getElementById("result").innerHTML = "You will pay $5092 plus $0.325 for each $1 over $45000.";
    let taxable_income = ((5092) + ((tax - 45000) * 0.325));
    let medicare_levy = tax * 0.02;
    let total_tax = taxable_income + medicare_levy;
    document.getElementById("result1").innerHTML = "You will pay " + total_tax + " dollars in tax, including Medicare levy."
    document.getElementById("result2").innerHTML = "Your Medicare levy is, in AUD: " + medicare_levy;
  } else if (tax in taxBracketThree) {
    document.getElementById("result").innerHTML = "You will pay $29467 plus $0.37 for every dollar over 120000";
    let taxable_income = ((29467) + ((tax - 120000) * 0.37));
    let medicare_levy = tax * 0.02;
    let total_tax = taxable_income + medicare_levy;
    document.getElementById("result1").innerHTML = "You will pay ", total_tax, " dollars in tax, including Medicare levy.";
    document.getElementById("result2").innerHTML = "Your Medicare levy is, in AUD: " + medicare_levy;
  } else if (tax in taxBracketFour) {
    document.getElementById("result").innerHTML = "You will pay $51667 plus $0.45 for each dollar over $180000.";
    let taxable_income = (51667) + ((tax - 180000) * 0.45);
    let medicare_levy = tax * 0.02;
    let total_tax = taxable_income + medicare_levy;
    document.getElementById("result1").innerHTML = "You will pay " + total_tax + " dollars in tax, including Medicare levy.";
    document.getElementById("result2").innerHTML = "Your Medicare levy is, in AUD: " + medicare_levy;
  } else {
    document.getElementById("result").innerHTML = "You have too much money for this calculator. Good luck!"
  }

}
<html>

<head>
  <script type="text/javascript" src="script.js"></script>
</head>

<body>

  <h2>Tax Calculator</h2>

  <h3>Enter your assessible income in AUD:</h3>
  <input type="text" id="income" name="income">
  <h3>Enter your allowable deductions in AUD:</h3>
  <input type="text" id="deductions" name="deductions">
  <button type="button" onclick="calculateTax()">Calculate my tax!</button>
  <p id="result"></p>
  <p id="result1"></p>
  <p id="result2"></p>
</body>

</html>

I am very very confused and would really appreciate some help. Thank you!

technophyle
  • 7,972
  • 6
  • 29
  • 50

1 Answers1

0

Your problem is misunderstanding the in operator. As described in the doc, in operator is to check whether an object contains a property or not.

Since arrays are also objects, the code above does not produce a compile error but the meaning of the operator is not what you expect. Basically what it means is that the array contains that property/key, which is the index in case of an array:

if (1 in [1, 2, 3]) {
  console.log('index 1 exists!);
}

if (1 in [4, 5, 6]) {
  // this condition also evaluates to true because the array also has the index range of 0-2, and 1 is among them.
  console.log('index 1 exists here too!);
}

In order to evaluate whether the value exists in the array, you need to use indexOf() or includes():

if ([1, 2, 3].includes(1)) {
  console.log('value 1 exists!');
}

if ([4, 5, 6].indexOf(1) > -1) {
  // this condition doesn't evaluate to true because there's no value 1 in the array
  console.log('value 1 exists here too!');
}
technophyle
  • 7,972
  • 6
  • 29
  • 50