-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathscript.js
More file actions
80 lines (67 loc) · 2.33 KB
/
script.js
File metadata and controls
80 lines (67 loc) · 2.33 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
document.addEventListener("DOMContentLoaded", initializeApp);
function initializeApp() {
// Define the coffee data
const coffees = [
{ id: 1, name: "Espresso", price: 2.5 },
{ id: 2, name: "Latte", price: 3.0 },
{ id: 3, name: "Cappuccino", price: 3.5 },
{ id: 4, name: "Americano", price: 2.0 },
{ id: 5, name: "Mocha", price: 3.75 },
];
renderCoffeeList(coffees);
attachFormListener();
}
// Renders the coffee list to the DOM
function renderCoffeeList(coffees) {
const coffeeListDiv = document.getElementById("coffeeList");
coffees.forEach((coffee) => {
const coffeeDiv = document.createElement("div");
coffeeDiv.className = "coffee-item";
coffeeDiv.innerHTML = `
<div class="coffee-label">
<input type="checkbox" id="coffee_${coffee.id}" data-price="${
coffee.price
}">
<label for="coffee_${coffee.id}">${
coffee.name
} (£${coffee.price.toFixed(2)})</label>
</div>
<input type="number" id="quantity_${
coffee.id
}" placeholder="Quantity" value="1" min="1">
`;
coffeeListDiv.appendChild(coffeeDiv);
});
}
// Attaches the submit event listener to the form
function attachFormListener() {
const coffeeForm = document.getElementById("coffeeForm");
coffeeForm.addEventListener("submit", handleFormSubmit);
}
// Handles the form submission event
function handleFormSubmit(event) {
event.preventDefault();
const total = calculateTotalOrderCost();
const totalCostDiv = document.getElementById("totalCost");
totalCostDiv.innerHTML = `<strong>Total Cost:</strong> £${total.toFixed(2)}`;
}
// Calculates the total cost based on selected items and their quantities
function calculateTotalOrderCost() {
let total = 0;
document
.querySelectorAll('input[type="checkbox"]:checked')
.forEach((checkedBox) => {
const quantityElement = document.getElementById(
`quantity_${checkedBox.id.split("_")[1]}`
);
const quantity = parseInt(quantityElement.value, 10);
// Handle cases where the quantity might not be a number or less than 1
if (isNaN(quantity) || quantity < 1) {
console.error(`Invalid quantity for ${checkedBox.id}: ${quantity}`);
return;
}
const price = parseFloat(checkedBox.getAttribute("data-price"));
total += quantity * price;
});
return total;
}