未完成的代码(简单工资单) - javascript

时间:2014-10-08 17:41:21

标签: javascript

请你帮我解决“othrs”(加班时间)中的公式

    var name, hrs, rate = 120, otrate = 150, othrs, bpay, otpay, gpay, ded, npay
    name = prompt("Enter your name")
    hrs = prompt("Enter hours worked")

    othrs = 
    bpay = hrs * rate
    otpay = othrs * otrate
    gpay = bpay + otpay
    ded = bpay * .10
    npay = gpay - ded

    if (hrs < 40) {
    hrs = 40
    }

    alert("Name: " + name + "\nNo. of hours worked: " + hrs + "\nNo. of overtime hours: " + othrs + "\nBasic pay: " + bpay + "\nOvertime pay: " + otpay + "\nGross pay: " + gpay + "\nDeduction: " + ded + "\nNet pay: " + npay)

1 个答案:

答案 0 :(得分:0)

好吧,既然这个问题没有结束,我也可以回答一下。在此处查看此操作:http://jsfiddle.net/jaozc7zd/

现在,这不是最有效的方法,我不打算为您重构代码,但这是您的答案:

var name, hrs, rate = 120, otrate = 150, othrs, bpay, otpay, gpay, ded, npay
name = prompt("Enter your name")
hrs = prompt("Enter hours worked")

othrs = hrs > 40 ? hrs - 40 : 0;
bpay = (hrs >= 40 ? 40 : hrs) * rate
otpay = othrs * otrate
gpay = bpay + otpay
ded = bpay * .10
npay = gpay - ded

alert("Name: " + name + "\nNo. of hours worked: " + hrs + "\nNo. of overtime hours: " + othrs + "\nBasic pay: " + bpay + "\nOvertime pay: " + otpay + "\nGross pay: " + gpay + "\nDeduction: " + ded + "\nNet pay: " + npay)