Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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;
about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!