Untitled
unknown
plain_text
3 years ago
1.5 kB
9
Indexable
// Define an array of computer products
let products = [
{
name: "Desktop Computer",
price: 999.99,
description: "Powerful desktop computer for all your computing needs."
},
{
name: "Laptop Computer",
price: 1499.99,
description: "Portable laptop computer with long battery life."
},
{
name: "Gaming Computer",
price: 2999.99,
description: "High-performance gaming computer for the most demanding games."
}
];
// Define a function to display the products on the website
function displayProducts() {
let productList = document.getElementById("product-list");
productList.innerHTML = "";
for (let i = 0; i < products.length; i++) {
let product = products[i];
let productItem = document.createElement("li");
productItem.innerHTML = `
<h2>${product.name}</h2>
<p>${product.description}</p>
<p>Price: $${product.price}</p>
<button onclick="addToCart(${i})">Add to Cart</button>
`;
productList.appendChild(productItem);
}
}
// Define a function to add a product to the shopping cart
function addToCart(index) {
let product = products[index];
let cartItem = document.createElement("li");
cartItem.innerHTML = `
<h2>${product.name}</h2>
<p>Price: $${product.price}</p>
`;
let cartList = document.getElementById("cart-list");
cartList.appendChild(cartItem);
}
// Call the displayProducts function when the page loads
window.onload = displayProducts;
Editor is loading...