-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
57 lines (45 loc) · 2 KB
/
script.js
File metadata and controls
57 lines (45 loc) · 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
document.addEventListener("DOMContentLoaded", function () {
const products = [
{ name: "Sports Shirt", price: 19.99, image: "img/product1.jpg" },
{ name: "Running Shoes", price: 59.99, image: "img/product8.jpg" },
{ name: "Hooded Jacket", price: 29.99, image: "img/product3.jpg" },
{ name: "Back Hat", price: 39.99, image: "img/product6.jpg" },
{ name: "Metal Bottle", price: 24.99, image: "img/product4.jpg" },
];
const productList = document.getElementById("product-list");
// Function to render products
function renderProducts(productArray) {
productList.innerHTML = "";
productArray.forEach(product => {
const card = document.createElement("div");
card.classList.add("product-card");
card.innerHTML = `
<img src="${product.image}" alt="${product.name}" class="product-img">
<h3>${product.name}</h3>
<p>$${product.price.toFixed(2)}</p>
<button class="buy-now-btn" onclick="buyNow('${product.name}')">Buy Now</button>
`;
productList.appendChild(card);
});
}
// Function to sort products based on price
function sortProducts(order) {
const sortedProducts = [...products];
if (order === 'asc') {
sortedProducts.sort((a, b) => a.price - b.price);
} else if (order === 'desc') {
sortedProducts.sort((a, b) => b.price - a.price);
}
renderProducts(sortedProducts);
}
// Initial rendering of products
renderProducts(products);
// Add event listeners for sorting buttons
const sortAscButton = document.getElementById("sort-asc");
const sortDescButton = document.getElementById("sort-desc");
sortAscButton.addEventListener("click", () => sortProducts("asc"));
sortDescButton.addEventListener("click", () => sortProducts("desc"));
});
function buyNow(productName) {
alert(`Thank you for buying ${productName}!`);
}