首页 > 解决方案 > 在 javascript 函数中计算销售税

问题描述

我无法计算税款,然后将其添加到小计中。只是为了让您知道我对一般编码非常陌生。目标是将轮胎价格、轮胎数量和轮胎费用全部加起来并征税,然后取消优惠券。另外,如果您有任何改进的建议,请告诉我,但正如我所说,请记住我对此很陌生。感谢您的时间 :)。

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>

标签: javascript

解决方案


您需要先弄清楚如何计算销售税。您可以查看其他资源,但这似乎是一个很好的例子。还有包容性和独家销售税。我不会进入那个——

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

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>


推荐阅读