-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
130 lines (96 loc) · 3.19 KB
/
script.js
File metadata and controls
130 lines (96 loc) · 3.19 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
console.log("Script is running!");
let cart = JSON.parse(localStorage.getItem("cart")) || [];
let slideIndex = 0;
function showSlides() {
let slides = document.getElementsByClassName("slide");
for (let i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slideIndex++;
if (slideIndex > slides.length) {
slideIndex = 1;
}
slides[slideIndex - 1].style.display = "block";
setTimeout(showSlides, 5000);
}
function changeSlide(n) {
slideIndex += n;
const slides = document.getElementsByClassName("slide");
if (slideIndex > slides.length) {
slideIndex = 1;
} else if (slideIndex < 1) {
slideIndex = slides.length;
}
for (let i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
slides[slideIndex - 1].style.display = "block";
}
document.addEventListener("DOMContentLoaded", showSlides);
function proceedToComplete() {
let cartItems = JSON.parse(localStorage.getItem("cart")) || [];
if (cartItems.length === 0) {
alert("Your cart is empty. Please add items before proceeding.");
} else {
localStorage.removeItem("cart");
window.location.href = "complete.html";
}
}
function addToCart(itemId) {
const itemElement = document.querySelector(`[data-item-id="${itemId}"]`);
const item = {
id: itemId,
name: itemElement.dataset.itemName,
price: parseFloat(itemElement.dataset.itemPrice),
quantity: 1,
};
const existingItem = cart.find((cartItem) => cartItem.id === itemId);
if (existingItem) {
existingItem.quantity++;
} else {
cart.push(item);
}
localStorage.setItem("cart", JSON.stringify(cart));
updateCartDisplay();
}
function updateCartDisplay() {
const cartItemsElement = document.getElementById("cart-items");
const totalCostElement = document.getElementById("total-cost");
cartItemsElement.innerHTML = "";
let totalCost = 0;
cart.forEach((item) => {
const listItem = document.createElement("li");
const quantityControls = document.createElement("div");
quantityControls.innerHTML = `
<button class="quantity-btn" onclick="changeQuantity(${item.id}, -1)">-</button>
<span>${item.quantity}</span>
<button class="quantity-btn" onclick="changeQuantity(${item.id}, 1)">+</button>
<button class="remove-btn" onclick="removeItem(${item.id})">Remove</button>
`;
listItem.innerHTML = `
<span>${item.name}</span>
${quantityControls.outerHTML}
<span>$${(item.price * item.quantity).toFixed(2)}</span>
`;
cartItemsElement.appendChild(listItem);
totalCost += item.price * item.quantity;
});
totalCostElement.textContent = totalCost.toFixed(2);
}
function changeQuantity(itemId, change) {
const itemIndex = cart.findIndex((item) => item.id === itemId);
if (itemIndex !== -1) {
cart[itemIndex].quantity += change;
if (cart[itemIndex].quantity < 1) {
cart[itemIndex].quantity = 1;
}
localStorage.setItem("cart", JSON.stringify(cart));
updateCartDisplay();
}
}
function removeItem(itemId) {
cart = cart.filter((item) => item.id !== itemId);
localStorage.setItem("cart", JSON.stringify(cart));
updateCartDisplay();
}
document.addEventListener("DOMContentLoaded", updateCartDisplay);