Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Javascript: Paycheck Calculator (having trouble getting the correct tax rate to show)

Hi all,

To practice my javascript skills, I took on a side project creating a paycheck calculator. I am running into issues with my 'if...else' statement because for the life of me, I can't get the correct tax rate to show based on the user's input. I wanted to get some additional eyes on it to see what I am missing.

Here is the HTML:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <form>
      <label for="contribution">Enter contribution percentage</label>
      <input type="number" id="contribution" name="contribution"><br><br>

      <label for="salary">Enter your annual salary:</label>
      <input type="text" id="salary" name="salary"><br><br>

      <label for="tax">Select your tax filing status:</label>
      <select id="tax">
        <option value="single">Single</option>
        <option value="married_joint">Married Filing Jointly</option>
        <option value="married_separately">Married Filing Separately</option>
        <option value="head_household">Head of Household</option>
      </select><br><br>

        <label for="paycheck_frequency">Paycheck Frequency</label>
        <select id="paycheck_frequency">
          <option value="weekly">Weekly</option>
          <option value="biweekly">Biweekly</option>
          <option value="twice_monthly">Twice a Month</option>
          <option value="monthly">Monthly</option>
      </select>
    </form>

    <button class="calculate">Calculate</button>

    <script src="main.js"></script>
  </body>
</html>

Here is the Javascript:

const userSalary = document.querySelector('#salary');
const button = document.querySelector('.calculate');

if (parseInt(userSalary.value) >= 500001) {
  var taxRate = 0.37;
} else {
  var taxRate = 0.10;
}

button.addEventListener('click', event => {
  console.log(parseInt(taxRate));
});

For whatever reason, only .1 is shown in the console no matter what I enter. Thanks in advance!

1 Answer

You are only running the javascript once, on load.

once you click the button, it takes the value that had been set since load (and because on load, the if condition resolved to false, that's the value it keeps, as you never do that evaluation again), so it will always print the same number no matter what.

You will want to wrap your if condition into a function, which you will then trigger with the click event listener, so that each time the button is clicked, the condition is re-evaluated.

good luck.

Thank you for your help! That makes sense.