-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproductquote.js
More file actions
43 lines (34 loc) · 1.2 KB
/
Copy pathproductquote.js
File metadata and controls
43 lines (34 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* Author: Kylie Drawdy
Date: 10/21/2024
*/
// Function to display a numeric value as a text string in the format $##.##
function formatCurrency(value) {
return "$" + value.toFixed(2);
}
let products = document.getElementsByClassName("productItem");
for ( let i = 0; i < products.length; i++) {
// added event listener and run function when item is clicked
products[i].addEventListener("click", calcTotal)
}
// create calcTotal Function
function calcTotal() {
// declare variables
let orderTotal = 0
let taxRate = 0.11;
// create for loop to loop through items
for (let i = 0; i < products.length; i++){
// create if statement to check if items are checked
if (products[i].checked){
// increase orderTotal by number of menu items
orderTotal += Number(products[i].value);
}
}
// calculate sales tax
let taxTotal = orderTotal * taxRate;
// calculate grand total
let grandTotal = orderTotal + taxTotal;
// add totals to page
document.getElementById("billTotal").innerHTML = formatCurrency(orderTotal);
document.getElementById("taxTotal").innerHTML = formatCurrency(taxTotal);
document.getElementById("grandTotal").innerHTML = formatCurrency(grandTotal);
}