Add JavaScript to your program to compute the total cost of the order. The total
ID: 3820296 • Letter: A
Question
Add JavaScript to your program to compute the total cost of the order. The total cost will be the sum of the price of each item times the quantity ordered times 1.07 to allow for taxes. Use a JavaScript confirm() call to display "The total cost of your order is (whatever the cost is)." The confirm() function displays a message box with your message and buttons for "OK" and "Cancel." It returns true of the "OK" button is clicked and false if the cancel button is clicked.
item1 = 39.99;
item2 = 99.99;
item3 = 24.99;
***User is required to enter quantity of each item in the order form. They are NOT prompted to enter.
Explanation / Answer
Hi,
Please see below the answer. Save this as Price.html. javascripts are written in between <script></script>tags.
Pleasecomment for any queries/feedbacks.
Thanks
<html>
<body>
<form>
<label id="Itme1lbl">Item 1($39.99)</label>
<input type="text" id="item1" name="item1"/> <br>
<label id="Itme2lbl">Item 2($99.99)</label>
<input type="text" id="item2" name="item2"/> <br>
<label id="Itme3lbl">Item 3($24.99)</label>
<input type="text" id="item3" name="item3"/> <br>
<button type="button">Total</button>
</form>
</body>
</html>
<script>
function calculate() {
var price1 = 39.99;
var price2 = 99.99;
var price3 = 24.99;
var qty1;
var qty2;
var qty3;
var tax=1.07;
var totPrice;
qty1 = document.getElementById('item1').value;
qty2 = document.getElementById('item2').value;
qty3 = document.getElementById('item3').value;
totPrice =(price1 * qty1) + (price2 * qty2) + (price3 * qty3);
totPrice = totPrice * tax;
var r = confirm("The total cost of your order is "+totPrice );
if (r == true) {
return true;
} else {
return false
}
}
</script>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.