0

I am trying to make a paycheck program, that utilizes functions and while loops. In this program, I have to create two functions, one for validating the pay rate and hours, and then one for the calculations. In addition, I have to have the first function pass the hours and pay rate to the calculation function, and then pass it back to the first function. When I try to run the program with the first function, it seems that if I enter a pay amount under 7.25, it enters an infinite loop.

Here is the code

     <script>
    function payValidate(x)
    {
        if(isNaN(payRate) || payRate < 7.25 || payRate > 20)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    function hoursValidate(x)
    {
        if(isNaN(hours) || hours < 1 || hours > 60)
        {
            return false;
        }
        else
        {
            return true;
        }

}
    var grossPay;
    var withHolding;
    var netPay;
    var message;



var payRate = parseInt(prompt("Enter pay rate"));

var payRateOK = payValidate(payRate);
    while(!payRateOK)
    {
        payRate = parseInt(prompt("Invalid pay rate. Enter pay rate again"));
        payRateOk = payValidate(payRate);
    }
var hours = parseFloat(prompt("Enter hours worked"));
var hoursOK = hoursValidate(hours);

while(!hoursOK)
{
    hours = parseFloat(prompt("Invalid hours. Enter hours again"));
    hoursOK = hoursValidate(hours);
}
grossPay = payRate * hours;

    if(grossPay <= 300)
    {
        withHolding = grossPay * 0.10;
    }
    else
    {
        withHolding = grossPay * 0.12;
    }
    netPay = grossPay - withHolding;


    var message = "Pay Rate: $" + payRate.toFixed(2) +
    "\nHours Worked: " + hours +
    "\nGross Pay $" + grossPay.toFixed(2) +
    "\nWithholding $" + withHolding.toFixed(2) +
    "\nNet Pay $" + netPay.toFixed(2);

    alert(message);
</script>
1
  • Since you have floats inside payValidate(), you should replace parseInt() with parseFloat(). Also use "use strict" and you will find your issue. Commented Dec 1, 2019 at 18:26

2 Answers 2

1

You're creating a new variable payRateOk (notice the lower case k) instead of writing to payRateOK, the variable you check in the while loop. So payRateOK will never change, and the loop will execute infinitely.

Sign up to request clarification or add additional context in comments.

Comments

0
var payRateOK = payValidate(payRate); // In here you have used "payRateOK"
while(!payRateOK)
{
   payRate = parseInt(prompt("Invalid pay rate. Enter pay rate again"));
   payRateOk = payValidate(payRate); // In here you have used "payRateok"
}
  • payRateOK != payRateOk there for you have to use same name for that

  • other thing is payRate is a float variable. you should use var payRate = parseFloat instead of var payRate = parseInt.

  • you have used hours as int type there for var hours = parseFloat should be var hours = parseInt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.