Skunkybuddy
Skunkybuddy

Reputation: 17

I am trying to write a JavaScript program that calculates gross pay, overtime, netpay and deducts taxes from the netpay based on dependants claimed

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".

Upvotes: 1

Views: 680

Answers (1)

Frish
Frish

Reputation: 1421

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;

Upvotes: 2

Related Questions