Skip to content

Commit 6ab2067

Browse files
committed
feat: Block Chain Collage Code 1 - 14.3
1 parent 00a4ae0 commit 6ab2067

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<section id="Products"></section>
10+
<script src="js/script.js"></script>
11+
</body>
12+
</html>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// const productContainer = document.querySelector("#Products")
2+
3+
// fetch("https://fakestoreapi.com/products") //fetch() starts an HTTP request to get data from that URL.
4+
// .then(res => res.json()) // .then(...) runs after the Promise succeeds
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+
// </div>
13+
// `
14+
// });
15+
// })
16+
// .catch(err => console.error("Error fetching products:",err)); // runs only if something goes wrong
17+
18+
// ________________________________________________
19+
20+
const numbers = [1, 2, 3, 4, 5];
21+
const doubled = numbers.map(num => num * 2);
22+
console.log("Original:", numbers);
23+
console.log("Doubled:", doubled);
24+
25+
// ________________________________________________
26+
27+
const productContainer = document.querySelector("#Products");
28+
29+
fetch("https://fakestoreapi.com/products") // fetch data
30+
.then(res => res.json())
31+
.then(data => {
32+
// create an array of HTML strings
33+
const productCards = data.map(product => `
34+
<div class="card">
35+
<img src="${product.image}" alt="${product.title}">
36+
<h2>${product.title}</h2>
37+
<p>${product.price}</p>
38+
</div>
39+
`);
40+
41+
// join array into one string and insert into container
42+
productContainer.innerHTML = productCards.join("");
43+
})
44+
.catch(err => console.error("Error fetching products:", err));

0 commit comments

Comments
 (0)