javascript读取工作小时数

时间:2016-05-24 14:47:41

标签: javascript

编写JavaScript以读取用户工作的小时数。然后编写JavaScript来计算用户在前40个小时工作时支付12美元/小时的费用,以及40个工作小时工作时间18美元/小时的费用。然后使用alert()函数将总金额打印到用户。

我必须使用什么代码

var y = prompt(“输入值”,“”);

2 个答案:

答案 0 :(得分:1)

Lol #OverComplicated。答案是重新制作一个更好的版本,然后在做勺子之前尝试做作业。

var BarryScott =  {
  PricePerHour: 12,
  HoursWorkedByBarry: 0,
  PrintPayment: function() {

    if ( this.HoursWorkedByBarry > 40) {
      var RemainHours = this.HoursWorkedByBarry - 40;
      alert(this.PricePerHour * 40 + RemainHours * 18);
    } else {
      alert(this.PricePerHour * this.HoursWorkedByBarry);
    } 
  },
  AskHoursFromBarry: function() {
    this.HoursWorkedByBarry = prompt("Enter Hours you worked");
    this.PrintPayment();
  }
}


BarryScott.AskHoursFromBarry();

答案 1 :(得分:1)

创建一个文件夹并将index.html和javascript代码放入其中。 运行index.html。

的index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Im lazy to do my assignment</title>
          <script src = "billhours.js"></script>
  </head>
  <body>
  </body>
</html>

billhours.js

var getInput = prompt("Enter Number of Hours worked");
var first40hrs = billHours(40, 12);
var over40hrs = billHours(getInput - 40, 18);
var totalSalary = first40hrs + over40hrs;

alert("Total Salary is "+totalSalary);

function billHours(hours, rate){
        return hours*rate;
}
//This function only works for hours 40 and above.
//It's your job to put conditional statements if hours is below 40. Keep Coding.