javascript - How to calculate a constant tax rate? -
this problem need resolve.
create basic webpage includes input form. javascript embedded html document.
the input form should include:
field user name input field order quantity input read field item price – set value of 5.31 read field order total cost “submit button” not use type of form button element user should able input name , order quantity, click “submit”. when “submit” clicked, form calculate order total, including sales tax of 8.25%. once calculated, order total form field updated total, including dollar sign , 2 decimal places. alert box should triggered stating users name , message indicating order total including sales tax.
be sure test own sample data verity calculations correct.
i did html , javascript can't calculate const taxrate of 8.25. item cost 5.31 , when tax included in total, huge amount calculated. in advance. please check script here:
<link rel="stylesheet" type="text/css" href="2examp.css"> <script type="text/javascript"> function calcorder() { const taxrate = 8.25; var username = document.getelementbyid("username").value; var quantity = document.getelementbyid("quantity").value; var cost = document.getelementbyid("cost").value; var extendedcost = quantity * cost; var taxamount = extendedcost * taxrate; var ordercost = extendedcost + taxamount; document.getelementbyid("costextended").value = "$" + ordercost.tofixed(2); alert("hello " + username + " - order of " + quantity + " widgets, total $" + ordercost.tofixed(2) + ", including tax."); } </script> </head> <body> <h1>lesson #2 - sample page</h1> <form action="" method="post" enctype="multipart/form-data" name="wigetcalc"> <label for="form-name">name</label> <input name="username" id="username" type="text" maxlength="15" /><br /> <label for="form-quantity">widget order quantity</label> <input name="quantity" id="quantity" type="text" value="0" maxlength="3" /><br /> <label for="cost">widget cost</label> <input name="cost" id="cost" type="text" value="5.31" readonly="readonly" /><br /> <label for="order-total">order total</label> <input name="costextended" id="costextended" type="text" readonly="readonly" /><br /> <p onclick="calcorder();" >process order</p> </form> </body> </html>
i believe issue multiplying 8.25 rather 0.0825.
you change
const taxrate = 8.25;
to
const taxrate = 0.0825;
or following:
var taxamount = extendedcost * (taxrate / 100);
Comments
Post a Comment