如何开始编写用于计算工资的表单的代码?

huangapple go评论60阅读模式
英文:

How do I start off the code for the form to calculate the payroll?

问题

Here's the translated part of the content you provided:

我是一个编程初学者,最近通过当地的一所学院参加了一个兼职课程,但我错误地选了一个面向第二学期的课程。我已经设法弄清楚了一些作业,但对于如何开始这个作业一无所知。(这门课程本应该在学习了两门Java课程之后才能选,而我甚至还没有碰过Java。)

以下是关于这一部分的视频内容,但我不确定如何将其应用到作业上,同时感觉好像它已经完成了。

这个作业有两个任务:

计算总工资

calcPay()函数开始:

通过将工资率乘以工作小时数来计算员工的工资。

如果工作时间超过40小时,那么按照50%的增加工资率来计算额外的工作时间。

例如,如果有人工作了45小时,时薪为10美元,那么40小时将乘以10美元,而5小时将乘以每小时15美元。

精度

printRow()函数中:

使用toFixed()方法,将总工资、税收和净工资变量转换为2位小数。

希望这有所帮助!如果需要更多信息,请告诉我。

英文:

I am a beginner at coding and recently took a part time course through a local college but I've also mistakenly taken a course meant for 2nd term.. I've managed to figure out some of the assignments but I am clueless on how to start off with this one. (This course was meant to be taken after 2 courses of learning java which I haven't even touched yet.)


The following script is what I've learned on a video in regards to this section but I'm not sure how to implement it onto the assignment but also feel like it's already been done.

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);

The 2 tasks in this assignment as follows:


**Calculate gross pay

Starting in the calcPay() function:

Calculate the employee’s pay by multiplying their rate of pay by the hours worked.

If the hours worked are greater than 40, then calculate those extra hours at an increased pay rate of 50%.

For example, if someone worked 45 hours at $10 an hour, 40 hours would be multiplied by $10, and 5 hours would be multiplied by $15 per hour.


**Precision
In the printRow() function:

Using the toFixed() method, convert the gross, tax, and net variables to be 2 decimal places.


<!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);
}

I've tried searching up different types of methods that I was able to find on how to calculate payroll but it seems that isnt what I'm required to do. What I think I need to do is just fill in the code that allows the payroll to be calculated and then added into a table below?

答案1

得分: 0

以下是代码的翻译部分:

// 计算薪水的示例
function calcPay(rate, hoursWorked) {
  let grossPay;

  if (hoursWorked <= 40) {
    grossPay = rate * hoursWorked;
  } else {
    grossPay = rate * 40 + (rate * 1.5) * (hoursWorked - 40);
  }

  return grossPay;
}

// 示例用法:
let rate = 10; // 每小时的美元
let hoursWorked = 45;
let totalPay = calcPay(rate, hoursWorked);

console.log(`在每小时 $${rate} 的情况下,工作 ${hoursWorked} 小时的总薪水为 $${totalPay}。`);
// 打印行的示例
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)}`);
}

希望这有所帮助!

英文:

An example for calcPay()

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function calcPay(rate, hoursWorked) {
  let grossPay;

  if (hoursWorked &lt;= 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}.`);

<!-- end snippet -->
An example for printRow()

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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)}`);
}

<!-- end snippet -->

Hope this helps!
-byteBat

huangapple
  • 本文由 发表于 2023年3月31日 03:14:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定