70.5k views
0 votes
Create a web page that uses JavaScript to print a payroll report for a company that has a few employees, all earning the same hourly wage: $15 per hour for up to 40 hours per week. If the employee worked more than 40 hours, the hours in excess of 40 are paid at one and a half times this rate.

User Kirk
by
4.3k points

1 Answer

7 votes

Answer:

  1. let employee1 = {
  2. Name : "Kate",
  3. Hour : 38,
  4. };
  5. let employee2 = {
  6. Name : "John",
  7. Hour : 45,
  8. };
  9. let employee3 = {
  10. Name : "Catherine",
  11. Hour : 40,
  12. };
  13. let employeeArr = [employee1, employee2, employee3];
  14. let payrollRef = document.getElementById("payroll");
  15. let output = "";
  16. for(let i = 0; i < employeeArr.length; i++){
  17. let h = employeeArr[i].Hour;
  18. let pay;
  19. if(h <=40){
  20. pay = h * 15;
  21. }else{
  22. pay = 40 * 15;
  23. pay += (h - 40) * 15 * 1.5;
  24. }
  25. output += "Employee Name: " + employeeArr[i].Name + "<br>";
  26. output += "Total working hour: " + employeeArr[i].Hour + "<br>";
  27. output += "Total pay: $" + pay + "<br>";
  28. output += "<hr>"
  29. }
  30. payrollRef.innerHTML = output;

Step-by-step explanation:

Presume there is a div element with id "payroll" in a html file. We can write a JavaScript to create a payroll report and place the output to the div.

In JavaScript, let's presume there are only three employees and their names and total working hour are recorded in three objects (Line 1 - 14). Next, we put those employee objects into an array, employeeArr (Line 16).

Next, create a for loop to traverse through each object from employeeArr and calculate the pay based on their working hour (Line 22 - 31).

Next, generate the output string which includes employee name, working hour and total pay (Line 33 -36).

At last, set the output string as the contents payroll (Line 39).

User JeremiahDotNet
by
4.2k points