1

I have the first half of the program running fine which gets the users hours worked and payrate and then prints the gross pay with overtime pay if there is any. But I need to print the net pay to the screen.

The second half of the program asks how many dependents the user claims and determines the tax rate from that answer. It then is supposed to deduct the taxes from the gross which would then be net pay.

Here is the code I have for the program

function myPay() {
var name = prompt("What is your name?"); 
var rate = parseInt(prompt("How much are you payed?"));
var hours = parseInt(prompt("How many hours do you work?"));
var depend = parseInt(prompt("How many dependents do you claim?"));
if (hours > 40 && rate < 20) 
{
var overtime = rate * 1.5 * (hours - 40); //Beginning of overtime.
var regular = rate * 40;
var pay = overtime + regular;
}
else
var pay = rate * hours; 
var net = pay * tax;   
if (depend = 0 && pay > 1000)
{
var tax = .33;
}
else if (depend = 0 && pay <= 1000)
{
var tax = .28;
}
else if (depend >= 1 && depend <= 3 && pay > 1000)
{
var tax = .25;
}
else if (depend >= 1 && depend <= 3 && pay <= 1000)
{
var tax = .22;
}
else if (depend >= 4 && depend <= 6 && pay > 1000)
{
var tax = .22;
}
else if (depend >= 4 && depend <= 6 && pay <= 1000)
{
var tax = .15;
}
else if (depend > 6 && pay > 1000)
{
var tax = .15;
}
else if (depend > 6 && pay <= 1000)
{
var tax = .10;
}
document.write("<p> your paycheck </p>" + net);
}

I am getting a NaN in the output section when I run this in the browser. I am not sure if this could be because of the format I am using for the variable "tax".

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Also, are you sure the `if` statement below `var net = pay * tax` is correct? It sets the `depend` variable to `0` and isn't a comparison. Perhaps change it to `if (depend == 0)`? – pxDav Feb 22 '22 at 00:05

1 Answers1

2

You are getting NaN because at this stage var net = pay * tax;, tax is undefined. Multiplying by undefined results in NaN.

Try instantiating tax outside of the if statement for DRY, and call it in the multiplication pay * tax afterwards:

var tax = 1; // so long as tax is not undefined, multiplication will return a number.
if(x) { tax = 0.22 }
elseif( ... )
var net = pay * tax;
Frish
  • 1,371
  • 10
  • 20
  • Does the else if need to be in there or could I just run it with the var net =pay * tax calculation after the tax = 0.22? Here is what I tried running and it still gave me a Nan error. Code : 'var tax; if (depend = 0 && pay > 1000) { tax = 0.33 } var net = pay * tax; document.write("

    your paycheck is.

    " + net);'
    – Skunkybuddy Feb 21 '22 at 21:40
  • Hmm, probably a better approach is to set tax to always be a number. I've updated the answer to be tax=1, so that if none of the conditions apply you're still not dealing with an undefined variable – Frish Feb 21 '22 at 23:51
  • You could also put a catch-all `else {...}` to set a default value for `tax` – Frish Feb 22 '22 at 00:14