用JavaScript函数计算营业税

时间:2019-01-18 23:16:13

标签: javascript

我在计算税收并将其添加到小计时遇到了麻烦。如此一来,您就知道我对编码总体而言还是一个新手。目标是将TirePrice,tireAmount和tireFees全部累加并征税,然后应将优惠券取走。另外,如果您有任何改进的秘诀,请告诉我,但正如我所说的,请记住,我对此很陌生。 谢谢您的时间:)。

function computePrice() {
    var tirePrice = document.getElementById('tirePrice').value;
    var tireAmount = document.getElementById('tireAmount').value;
    var tireFees = document.getElementById('tireFees').value;
    var tireCoupons = document.getElementById('tireCoupons').value;
    var finalPrice = ((+tirePrice * +tireAmount + +tireFees) * .8 - +tireCoupons).toFixed(2);
    finalPrice = finalPrice.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    document.getElementById('finalPrice').innerHTML = "Total:$" + finalPrice;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>BJ's Tire Bay Calculator</title>
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>

<body>

    <h1>BJ's Tire Bay Calculator </h1>
    <p>Price Per Tire: $ <input id="tirePrice" type="number" min="1" max="100000"></p>
    <p>Number Of Tires: <input id="tireAmount" type="number" min="1" max="4"></p>
    <p>Installation Fee and NYS Recycling Fee: $<input id="tireFees" type="number" min="1" max="70"></p>
    <p>Coupons: $ <input id="tireCoupons" type="number" max="100000"></p>
    <input id="displayNum" value="Calculate" type="button" onclick="computePrice()">
    <h2 id="finalPrice"></h2>
</body>
<script src="index.js"></script>

</html>

1 个答案:

答案 0 :(得分:0)

您需要先弄清楚如何计算营业税。您可以研究其他资源,但是this one似乎是一个很好的例子。还有包含性和排他性的营业税。我不明白-

一旦有了数学,就可以做类似的事情:

const calculateSalesTax = (amount, taxPercent) => {
  // Whatever math is involved to calculate your sales tax here
  return amount + (taxPercent * amount);
};

document.getElementById('calculate').onclick = () => {
  const amount = document.getElementById('amount').value;
  const tax = document.getElementById('percent').value;
  document.getElementById('total').innerText = `$ ${calculateSalesTax(+amount, +tax)}`;
};
<label for="amount">Amount</label>
<input type="number" placeholder="amount" id="amount" />

<label for="percent">Sales Tax</label>
<input type="number" placeholder="percent" id="percent" />

<button id="calculate">Calculate</button>
<div id="total"></div>