-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinals.js
54 lines (47 loc) · 1.56 KB
/
finals.js
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
// Initialize cart items and total price
var cartItems = [];
var totalPrice = 0;
// Function to add items to the cart
function addToCart(product, price) {
cartItems.push({ name: product, price: price });
totalPrice += price;
alert(product + " has been added to the cart!");
updateCart();
}
// Function to display items in the cart
function displayCart() {
var cartElement = document.getElementById("cart");
cartElement.innerHTML = ""; // Clear previous content
// Create a list of cart items
cartItems.forEach(function(item) {
var itemElement = document.createElement("p");
itemElement.textContent = item.name + " - P" + item.price;
cartElement.appendChild(itemElement);
});
// Display total price
var totalElement = document.createElement("p");
totalElement.textContent = "Total: P" + totalPrice;
cartElement.appendChild(totalElement);
}
// Function to handle checkout
function checkout() {
if (cartItems.length === 0) {
alert("Your cart is empty!");
} else {
alert("Thank you for your purchase! Total Price: P" + totalPrice);
// Reset cart after checkout
cartItems = [];
totalPrice = 0;
updateCart();
displayCart();
}
}
// Function to update cart display icon
function updateCart() {
var cartIcon = document.getElementById("car-icon");
cartIcon.textContent = " " + cartItems.length;
}
// Function to alert items in the cart (alternative displayCart)
function alertCartContents() {
alert("Products in cart: " + cartItems.map(item => item.name).join(", "));
}