简体   繁体   中英

Calculating Sales Tax in a javascript function

I am having trouble calculating tax and then adding it to the subtotal. Just so you are aware I am very new to coding in general. the goal is to have the tirePrice,tireAmount, and tireFees all added up and taxed and then the coupons should be taken off. also if you have any tips to improve let me know but as I said keep in mind I am very new to this. Thank you for your time :).

 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> 

You need to figure out how you're going to calculate sales tax first. You can look into other resources but this one seems to give a good example. There's also inclusive and exclusive sales tax. I'm not getting into that --

Once you have that math, you can then do something similar:

 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM