-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
40 lines (34 loc) · 1.13 KB
/
script.js
File metadata and controls
40 lines (34 loc) · 1.13 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
const products = [
{ name: "Smartphone", category: "Electronics", price: 299 },
{ name: "Laptop", category: "Electronics", price: 799 },
{ name: "T-shirt", category: "Clothing", price: 20 },
{ name: "Headphones", category: "Electronics", price: 99 },
{ name: "Jeans", category: "Clothing", price: 50 },
];
function displayProducts(productArray) {
const productList = document.getElementById("product-list");
productList.innerHTML = ""; // Clear the current list
productArray.forEach((product) => {
const productItem = document.createElement("div");
productItem.classList.add("product-item");
productItem.innerHTML = `
<h3>${product.name}</h3>
<p>Category: ${product.category}</p>
<p>Price: $${product.price}</p>
`;
productList.appendChild(productItem);
});
}
function filterProducts(category) {
if (category === "All") {
displayProducts(products);
} else {
const filteredProducts = products.filter(
(product) => product.category === category
);
displayProducts(filteredProducts);
}
}
window.onload = () => {
displayProducts(products);
};