编程新手求助:如何完成工资计算表单的代码编写?
薪资计算表单作业求助
我是编程新手,误选了需要Java先修课的第二学期课程,现在卡在了作业上。已经完成部分代码,但不知道怎么完成剩下的calcPay和printRow函数,作业要求如下:
作业任务
- 计算总工资:在
calcPay()函数中,员工工资按小时工资×工作时长计算;若工作时长超过40小时,超出部分按1.5倍小时工资计算(例:45小时、时薪10美元,40小时按10美元/小时,5小时按15美元/小时)。 - 精度处理:在
printRow()函数中,使用toFixed()方法将gross、tax、net变量转换为两位小数。
我从课程视频里学到了这段DOM操作的代码,但不知道怎么套用:
function checkData(){// Validate the data// Retrieve the data from the DOM let name = document.getElementById("name").value; let mark = document.getElementById("mark").value; mark = parseFloat(mark); if(name != "" && isNaN(mark)==false){ displayData(name,mark); resetFields(); function displayData(name,mark){// Declare variables let passfail = "pass"; // Create the elements using DOM methods let newTr = document.createElement("tr"); let newNameTd = document.createElement("td"); let newMarkTd = document.createElement("td"); let newPFTd = document.createElement("td"); if(mark < 50){ passfail = "fail"; // Add the data to the tds newNameTd.innerHTML = name; newMarkTd.innerHTML = mark; newPFTd.innerHTML = passfail; newTr.appendChild(newNameTd); newTr.appendChild(newMarkTd); newTr.appendChild(newPFTd);
现有HTML代码
<!DOCTYPE html><html> <head> <script src="script.js"></script> <link rel="stylesheet" type="text/css" href="styles.css" /> <meta charset="utf-8" /> <title>Competency 17</title> </head> <body> <section id="content"> <h1> Employee Payroll Entry Form </h1> <form id="payForm"> <label for="">Full Name:</label><input type="text" autofocus id="fullName"/> <br /> <label for="">Hours Worked:</label><input type="text" id="hoursWorked" /><br /> <label for="">Hourly Rate:</label><input type="text" id="hourlyRate" /> <footer> <button id="calcButton" type="button">Calculate</button> </footer> </form> <h1> Employee Payroll Summary Report </h1> <table id = "employees"> <thead> <tr> <th>Employee Name</th> <th>Gross Pay</th> <th>Tax</th> <th>Net Pay</th> </tr> </thead> <tbody> </tbody> </table> </section> </body></html>
现有JavaScript代码
document.addEventListener("DOMContentLoaded", load); function load(){ let calcButton = document.getElementById('calcButton'); calcButton.addEventListener('click', calc); clearFields(); } function calc(){ let name = document.getElementById('fullName').value; let hours = document.getElementById('hoursWorked').value; let rate = document.getElementById('hourlyRate').value; if(name == "" || hours =="" || rate==""){ clearFields(); return; } let pay = calcPay(hours,rate); let taxes = getTax(pay); let net = pay-taxes; printRow(name, pay, taxes, net); clearFields(); } function clearFields(){ document.getElementById('fullName').value = ""; document.getElementById('hoursWorked').value = ""; document.getElementById('hourlyRate').value = ""; document.getElementById('fullName').focus(); } /* calcPay function receives hours and hourly rate values returns the calculated pay*/ function calcPay(hours, rate){ let } /* getTax function receives gross pay returns relative tax rate*/ function getTax(funcGross){ let funcTax = 0; if (funcGross<250){ funcTax = funcGross*.25; } else{ if (funcGross<500){ funcTax = funcGross*.3; } else{ if (funcGross<=750){ funcTax = funcGross*.4; } else{ funcTax = funcGross*.5; } } }return funcTax; } /* printRow function receives name, gross, taxes, and net pay formats currency prints a row in the table*/ function printRow(name, gross, taxes, net){ // Set values to 2 decimal places here let tbody = document.getElementsByTagName("tbody")[0]; let tr = document.createElement("tr"); let td = document.createElement("td"); let td1 = document.createElement("td"); let td2 = document.createElement("td"); let td3 = document.createElement("td"); td.innerHTML = name; td1.innerHTML = `$${gross}`; td2.innerHTML = `$${taxes}`; td3.innerHTML = `$${net}`; tr.appendChild(td); tr.appendChild(td1); tr.appendChild(td2); tr.appendChild(td3); tbody.appendChild(tr); }
我搜索了很多薪资计算方法,但都不符合要求,希望有人能帮我补全calcPay和printRow函数的代码,实现薪资计算并把结果添加到表格里。
内容的提问来源于stack exchange,提问作者Codejester42
相关产品推荐
相关产品推荐

