簡體   English   中英

不知道我的簡單代碼怎么了

[英]Not sure what's wrong with my simple code

我不確定我在簡單的營業稅計算器中做錯了什么。 當我按Submit時,我想要顯示項目成本加營業稅的美元金額,但我看到的total tip $functionround(){[native code]}

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

  total = Math.round
total = Math.round

在上面的行中,您正在將函數Math.round的值分配給變量total。 相反,您可能想將Math.round函數返回的值分配給您的總變量,如下所示:

total = Math.round(total)

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

您應該在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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM