简体   繁体   中英

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

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;

EDIT: I have changed the code according to your comment.

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?"));
    // Set variable here for later to be used (Preventing out of scope errors.)
    var pay;
    var tax;
    if (hours > 40 && rate < 20) {
        var overtime = rate * 1.5 * (hours - 40); //Beginning of overtime.
        var regular = rate * 40;
        pay = overtime + regular;
    } else {
        pay = rate * hours;
    }
    
    if (depend == 0 && pay > 1000) {
        tax = .33;
    } else if (depend == 0 && pay <= 1000) {
        tax = .28;
    } else if (depend >= 1 && depend <= 3 && pay > 1000) {
        tax = .25;
    } else if (depend >= 1 && depend <= 3 && pay <= 1000) {
        tax = .22;
    } else if (depend >= 4 && depend <= 6 && pay > 1000) {
        tax = .22;
    } else if (depend >= 4 && depend <= 6 && pay <= 1000) {
        tax = .15;
    } else if (depend > 6 && pay > 1000) {
        tax = .15;
    } else if (depend > 6 && pay <= 1000) {
        tax = .10;
    }
    var net = pay - (pay * tax);
    document.write("<p> your paycheck </p>" + net);
}
myPay();

Results

Name: n
How much are you payed: 1000
How many hours do you work: 1
How many dependents do you claim: 7

Paycheck: 900

I'm sorry if I am wrong again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM