不知道我的简单代码怎么了

时间:2019-07-02 01:34:39

标签: javascript html css calculator

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

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

  total = Math.round

3 个答案:

答案 0 :(得分:1)

total = Math.round

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

total = Math.round(total)

答案 1 :(得分:0)

如上所述,您需要返回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>

答案 2 :(得分:0)

您应该在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>