javascript - 在 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>
解决方案
您需要先弄清楚如何计算销售税。您可以查看其他资源,但这似乎是一个很好的例子。还有包容性和独家销售税。我不会进入那个——
一旦你有了这个数学,你就可以做类似的事情:
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>
推荐阅读
- c# - 在 Angular 中使用 c# 模型类
- css - css flex-wrap flex-basis 是不够的
- git - Github:由于在本地找不到远程工作,无法推送到远程分支;--force 也不起作用
- javascript - 是否可以从电子表格生成嵌套的 JSON?
- github - 在 GitHub 中为新存储库上传文件
- c# - Transactional batch cosmos - 写入 IF 在分区中读取的某个值
- python - 使用 Django 根据用户选择有条件地更改背景图像?
- html - 使图像从上到下连续显示
- java - 无法使用 Java Graph SDK 读取流以获取附件
- java - 为什么在转换为 JSON 时相关实体会被转义?