1. In a separate JS file, write a function to calculate gross wages for an hourly employee. The function should receive two parameters: the pay rate (dollars per hour, a floating point number) and the hours worked. Hours from 0 through 40 are “straight time” pay, and any hours in excess of 40 are overtime pay at 1.5 times the standard rate (hint: use the ? : operator to make a decision about pay rates). The function should return a floating point number representing the gross pay amount. Note, the function should not do any output such as document.writeln or alerts.
2. In a separate JS file, write a function to compute the amount of taxes that should be withheld from a paycheck based on the gross pay. The function should receive one parameter, the gross pay. Withholdings are calculated based on the following table:
Category |
Percentage |
Federal taxes |
31% |
State taxes |
8% |
Local taxes |
2% |
All local taxes are deductable from the gross amount used to calculate state taxes. Likewise all state and local taxes are deductable from the gross amount used to calculate federal taxes. The function should accept one parameter, the gross pay, and return one result, the total withholdings based on the above table and rules. Again, the function should not do any output, but should just return a number. Here is an example:
Input: 1000.00, return 377.90. Why not just 41% of 1000 = 410? 2% of $1000.00 is $20.00. Now subtract the $20.00 out of the gross, leaving $980.00 as the base for calculating state taxes. 8% of $980.00 is $78.40. Subtract $78.40 from the $980.00 to get $901.60, the base for calculating federal taxes. 31% of $901.60 is $279.50 (rounded to the nearest penny). When $20.00, $78.40, and $279.50 are added together, the result is $377.90, which is the answer that should be returned from the function. Note that these numbers are rounded to the nearest penny, which is possible with either the .toFixed() method of a number or by using Math.round().
(1) JS File:
//Function that calculates gross pay
function calculateGrossWages(payrate, hoursWorked)
{
var totalPay;
//Using Ternary Operator(?=) for calculation of total
pay
totalPay = (hoursWorked > 40) ? ( (40 *
payrate) + (1.5 * (hoursWorked-40) * payrate) ) : (hoursWorked *
payrate);
//Returning totalPay
return totalPay;
}
Sample Run:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
(2) Js File:
//Function that calculates gross pay
function computeTaxDeductable(grossPay)
{
var holdings = 0;
//Calculating local taxes
localTax = grossPay * (2/100.0);
//Updating holdings
holdings += localTax;
//Deducting local taxes
grossPay -= localTax;
//Calculating State taxes
stateTax = grossPay * (8/100.0);
//Updating holdings
holdings += stateTax;
//Deducting state taxes
grossPay -= stateTax;
//Calculating federal taxes
federalTax = grossPay * (31/100.0);
//Updating holdings
holdings += federalTax;
//Deducting local taxes
grossPay -= federalTax;
//Returning total holdings
return holdings.toFixed(2);
}
Sample Run:
Get Answers For Free
Most questions answered within 1 hours.