我是一名编码初学者,最近参加了当地一所大学的兼职课程,但我也错误地参加了第二学期的课程。。我已经设法弄清楚了一些作业,但我不知道如何从这个开始。(这门课是在学习了两门java课程后才上的,我还没有接触过。)
下面的脚本是我在视频中学到的关于这一部分的内容,但我不确定如何在作业中实现它,但我觉得它已经完成了。
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);
本任务中的2项任务如下:
**计算总支付
在calcPay()函数中启动:
通过将员工的工资率乘以工作时间来计算员工的工资。
如果工作时间超过40小时,则按50%的加薪率计算这些额外的工作时间。
例如,如果有人以每小时10美元的价格工作45小时,40小时将乘以10美元,5小时将乘以每小时15美元。
**精度在printRow()函数中:
使用toFixed()方法,将总变量、税收变量和净变量转换为小数点后2位。
<!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>
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()示例
function calcPay(rate, hoursWorked) {
let grossPay;
if (hoursWorked <= 40) {
grossPay = rate * hoursWorked;
} else {
grossPay = rate * 40 + (rate * 1.5) * (hoursWorked - 40);
}
return grossPay;
}
// Example usage:
let rate = 10; // dollars per hour
let hoursWorked = 45;
let totalPay = calcPay(rate, hoursWorked);
console.log(`The gross pay for ${hoursWorked} hours at $${rate} per hour is $${totalPay}.`);
printRow()示例
function printRow(name, rate, hoursWorked) {
let grossPay = calcPay(rate, hoursWorked);
let tax = grossPay * 0.15;
let netPay = grossPay - tax;
console.log(`${name.padEnd(10)} ${rate.toFixed(2).padStart(6)} ${hoursWorked.toString().padStart(6)} ${grossPay.toFixed(2).padStart(8)} ${tax.toFixed(2).padStart(6)} ${netPay.toFixed(2).padStart(8)}`);
}
希望这能有所帮助!-字节蝙蝠