|
| 1 | +// const productContainer = document.querySelector("#Products"); |
| 2 | + |
| 3 | +// fetch("https://fakestoreapi.com/products") |
| 4 | +// .then((res) => res.json()) |
| 5 | +// .then((data) => { |
| 6 | +// data.forEach((product) => { |
| 7 | +// productContainer.innerHTML += ` |
| 8 | +// <div class="card"> |
| 9 | +// <img src="${product.image}" alt="${product.title}"> |
| 10 | +// <h2>${product.title}</h2> |
| 11 | +// <p>${product.price}</p> |
| 12 | +// <p>${product.description}</p> |
| 13 | +// </div> |
| 14 | +// `; |
| 15 | +// }); |
| 16 | +// }) |
| 17 | +// .catch(err => console.log("Error fetching products",err)) |
| 18 | + |
| 19 | +// const numbers = [1, 2, 3, 4, 5]; |
| 20 | +// const doubled = numbers.map((num) => num * 2); |
| 21 | +// console.log("Original:", numbers); |
| 22 | +// console.log("Doubled:", doubled); |
| 23 | + |
| 24 | +const productContainer = document.querySelector("#Products"); |
| 25 | + |
| 26 | +fetch("https://fakestoreapi.com/products") |
| 27 | + .then((res) => res.json()) |
| 28 | + .then((data) => { |
| 29 | + const productCards = data.map( |
| 30 | + (product) => |
| 31 | + ` |
| 32 | + <div class="card"> |
| 33 | + <img src="${product.image}" alt="${product.title}"> |
| 34 | + <h2>${product.title}</h2> |
| 35 | + <p>${product.price}</p> |
| 36 | + <p>${product.description}</p> |
| 37 | + </div> |
| 38 | +`, |
| 39 | + ); |
| 40 | + productContainer.innerHTML = productCards.join(""); |
| 41 | + }) |
| 42 | + .catch((err) => console.log("Error fetching products", err)); |
0 commit comments