-1

我在使用 javascript 时遇到了很多麻烦。我应该使用函数制作一个简单的薪水程序。我不知道我做错了什么。我还必须将payrateand更改taxrate为十进制格式。你怎么把它放在代码中?这是我的代码的样子。

<html>
  <body bgcolor="#81DAF5">
  <head>
    <title>Chapter 4 Assignment 1</title>
  </head>
  <body>
    <script type="text/javascript">
      var hoursworked = window.prompt("How many hours worked?", "");
      var payrate = window.prompt("What is the pay rate?", "");
      var taxrate = window.prompt("What is the tax rate?", "");
      var netpay
      parseFloat(payrate).toFixed(2);
      parseFloat(taxrate).toFixed(2);
      function calculatepay()
      {
        var grosspay= hoursworked * payrate;
        var taxamount= (grosspay * taxrate) / 100;
        var netpay= grosspay - taxamount;
        return netpay;
      }

      document.write("<h1><b>Hours Worked: " +hoursworked+ "<br></h1></b>");
      document.write("<h1><b>Hourly payrate: " +payrate+ "<br></h1></b>");
      document.write("<h1><b>Tax rate applied: " +taxrate+ "<br></h1></b>");
      document.write("<h1><b>Net Pay = " +calculatepay+ "<br></h1></b>");
    </script>
  </body>
</html>
4

4 回答 4

1

代码中的问题很少

  1. 解析输入字符串后,需要将返回值赋回变量 expayrate = parseFloat(payrate).toFixed(2);
  2. 您需要通过在末尾calculatepay添加来调用该函数以打印结果()

尝试

  var hoursworked = window.prompt("How many hours worked?", "");
  var payrate = window.prompt("What is the pay rate?", "");
  var taxrate = window.prompt("What is the tax rate?", "");
  payrate = parseFloat(payrate).toFixed(2);
  taxrate = parseFloat(taxrate).toFixed(2);
  function calculatepay()
  {
    var grosspay= hoursworked * payrate;
    var taxamount= (grosspay * taxrate) / 100;
    var netpay= grosspay - taxamount;
    return netpay;
  }

  var netpay = calculatepay();

  document.write("<h1><b>Hours Worked: " +hoursworked+ "<br></h1></b>");
  document.write("<h1><b>Hourly payrate: " +payrate+ "<br></h1></b>");
  document.write("<h1><b>Tax rate applied: " +taxrate+ "<br></h1></b>");
  document.write("<h1><b>Net Pay = " +netpay+ "<br></h1></b>");

演示:Plunker

于 2013-06-24T03:18:00.883 回答
0

我已经为您更新了一些脚本。一些小错误。调用计算支付函数时没有包含括号,也没有存储应用 parseFloat 方法的结果。

应用括号给出:

document.write("<p>Net Pay = " + calculatepay() + "</p>"); 

存储值:

payrate = parseFloat(payrate).toFixed(2);
taxrate = parseFloat(taxrate).toFixed(2);

你也不需要单独声明每个变量,你可以通过简单地用逗号分隔它们来将它们作为一个组来完成:

var hoursworked = window.prompt("How many hours worked?", ""),
    payrate = window.prompt("What is the pay rate?", ""),
    payrate = parseFloat(payrate).toFixed(2),
    taxrate = window.prompt("What is the tax rate?", ""),
    taxrate = parseFloat(taxrate).toFixed(2),
    netpay = 0;

这是一个工作示例 - http://jsfiddle.net/2UVyH/2

希望有帮助

于 2013-06-24T03:30:57.190 回答
0

感谢更新。虽然parseFloat(payrate).toFixed(2)确实似乎将存储的值转换为浮点数,但它将答案存储为字符串

此外,您可能应该将结果分配给另一个变量或payrate在任何情况下返回,否则浮点数/字符串将丢失。

尝试:

payrate = parseFloat(payrate).toFixed(2);
taxrate = parseFloat(taxrate).toFixed(2);

然后在你的calculatepay()函数中:

// neat trick at http://stackoverflow.com/a/5661399/1091386
// allows you to use a string as a float in addition/subtraction
// apparently not necessary with multiplication but I thought I'd leave it in

var taxamount = hoursworked * +(payrate);

最后,不要忘记calculatepay()在最后使用 the调用,()否则您将只返回该函数。

于 2013-06-24T03:11:25.887 回答
0

当你使用 parseFloat(payrate).toFixedString(2); 实际值不变。

于 2013-06-24T03:11:45.663 回答