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