Javascript功能麻烦

时间:2013-06-24 03:01:29

标签: javascript function

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

<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 个答案:

答案 0 :(得分:1)

代码中存在一些问题

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

      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

答案 1 :(得分:0)

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

此外,您应该将结果分配给另一个变量,或者在任何情况下都返回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()拨打(),否则您将只返回该功能。

答案 2 :(得分:0)

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

答案 3 :(得分: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

希望有所帮助