【问题标题】:Not sure what's wrong with my simple code不知道我的简单代码有什么问题
【发布时间】:2019-11-12 14:48:23
【问题描述】:

我不确定我在简单的销售税计算器中做错了什么。当我按下提交时,我希望显示一美元的商品成本加上销售税,但我看到的是total tip $functionround(){[native code]}

  //calculation
  var total = (itemCost * salesTax + itemCost);

  total = Math.round

【问题讨论】:

  • total = Math.round 会将total 设置为“本机”Math.round 函数的定义。

标签: javascript html css calculator


【解决方案1】:
total = Math.round

在上面的行中,您将函数 Math.round 的值分配给变量 total。相反,您可能希望将函数 Math.round 返回的值 分配给您的总变量,如下所示:

total = Math.round(total)

【讨论】:

    【解决方案2】:

    您应该考虑calculation 上的这些代码。这是一个简单的税收计算器,效果很好:

        function fmtPrice(value) {
                    result="$"+Math.floor(value)+".";
                    var cents=100*(value-Math.floor(value))+0.5;
                    result += Math.floor(cents/10);
                    result += Math.floor(cents%10);
                    return result;
                    }
                    function compute() {
                    var unformatted_tax = (document.forms[0].cost.value)*(document.forms[0].tax.value);
                    document.forms[0].unformatted_tax.value=unformatted_tax;
                    var formatted_tax = fmtPrice(unformatted_tax);
                    document.forms[0].formatted_tax.value=formatted_tax;
                    var cost3= eval( document.forms[0].cost.value );
                    cost3 += eval( (document.forms[0].cost.value)*(document.forms[0].tax.value) );
                    var total_cost = fmtPrice(cost3);
                    document.forms[0].total_cost.value=total_cost;
                    }
                    function resetIt() {
                    document.forms[0].cost.value="19.95"; // cost of product
                    document.forms[0].tax.value=".06"; // tax value 
                    document.forms[0].unformatted_tax.value="";
                    document.forms[0].formatted_tax.value="";
                    document.forms[0].total_cost.value="";
                    }
    <CENTER>
    <FORM>
    <TABLE BORDER=2 WIDTH=300 CELLPADDING=3>
    <TR>
    <TD align="center"><FONT SIZE=+1><STRONG>Cost</STRONG></FONT>
    <TD align="center"><FONT SIZE=+1><STRONG>Tax</STRONG></FONT>
    </TR>
    <TR>
    <TD align="center"><INPUT TYPE="text" NAME="cost" VALUE="19.95" SIZE=10>
    <TD align="center"><INPUT TYPE="text" NAME="tax" VALUE=".06" SIZE=10>
    </TR>
    </TABLE>
    <BR>
    <TABLE BORDER=1 WIDTH=600 CELLPADDING=3>
    <TR>
    <TD align="center"><FONT SIZE=+1><STRONG>Unformatted Tax</STRONG></FONT>
    <TD align="center"><FONT SIZE=+1><STRONG>Formatted Tax</STRONG></FONT>
    <TD align="center"><FONT SIZE=+1><STRONG>TOTAL COST</STRONG></FONT>
    </TR>
    <TR>
    <TD align="center"><INPUT TYPE="text" NAME="unformatted_tax" SIZE=15>
    <TD align="center"><INPUT TYPE="text" NAME="formatted_tax" SIZE=15>
    <TD align="center"><INPUT TYPE="text" NAME="total_cost" SIZE=15>
    </TR>
    </TABLE>
    <BR>
    <TABLE BORDER=0 WIDTH=400 CELLPADDING=5>
    <TR>
    <TD align="center"><INPUT TYPE="reset"  VALUE="RESET" onClick="resetIt()">
    <TD align="center"><INPUT TYPE="button" VALUE="COMPUTE" onclick="compute()">
    </TR>
    </TABLE>
    </CENTER>

    【讨论】:

    • 已弃用,不应使用。表格是 1990 年代的,不应该用于布局。海事组织
    • 哦,我明白了。我只是想做一个演示。人们现在倾向于做古老的事情。
    【解决方案3】:

    如前所述,您需要返回 Math.round 的总和 - 但您还需要将值解析为数字),然后您还必须记住销售税是一个百分比 - 所以它必须除以100.

    我已将您的逻辑修改为

    a) 使用 parseInt() 将输入的值解析为数字

    b) 解决 math.round() 问题

    c) 通过将商品成本乘以销售税百分比来获得销售税值 ... itemCost * (salesTax/100)

    d) 将销售税值添加到商品成本...商品成本 + (itemCost * (salesTax/100))...

    //Function
    function calculateTip() {
      var itemCost = parseInt(document.getElementById("itemCost").value);
      var salesTax = parseInt(document.getElementById("salesTax").value);
      //enter values window
      if (itemCost === "" || salesTax == "") {
        window.alert("Please enter the values!");
        return;
      }
      //calculation
      var total = Math.round(itemCost + (itemCost * salesTax/100));
    
      //display amount
      document.getElementById("totalTip").style.display = "block";
      document.getElementById("amount").innerHTML = total;
    }
    //Hide Tip Amount and call our function with a button
    document.getElementById("totalTip").style.display = "none";
    
    document.getElementById("submit").onclick = function() {
      calculateTip();
    };
    </head>
    
    <body id="color">
      <div class="container" id="contain">
        <div class="text-center">
          <h1>Sales Tax Calculator</h1>
          <p> Amount Before Tax?</p>
          $ <input id="itemCost" type="text" placeholder="item cost">
          <p>Sales Tax Percentage?</p>
          <input id="salesTax" type="text" placeholder="sales tax percent"><br><br>
    
          <button type="submit" id="submit">submit</button>
        </div>
        <div class="container" ID="totalTip">
          <div class="text-center">
            <p>Total Tip</p>
            <sup>$</sup><span id="amount">0.00</span>
          </div>
        </div>
      </div>
      <script type="text/javascript" src="javascript.js"></script>
    </body>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-19
      • 2022-07-27
      • 1970-01-01
      • 2012-05-19
      相关资源
      最近更新 更多