-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome1.js
82 lines (76 loc) · 2.39 KB
/
home1.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// let fetchData = async ()=>{
// let data = await fetch("https://fakestoreapi.com/products")
// console.log(data)
// let output = await data.json()
// console.log(output)
// // console.log(output[0])
// }
// fetchData()
let user = document.getElementById("welcome2")
user.innerHTML=localStorage.getItem("username")
// fetch("https://fakestoreapi.com/products")
// .then((res)=>{
// res.json()
// .then((output)=>{
// output.forEach((ele)=>{
// let container = document.getElementById("card_container")
// container.innerHTML+=`
// <main class="card">
// <img src="${ele.image}">
// <h1>${ele.title}</h1>
// <p>Description: ${ele.description.slice(0,100)}</p>
// <b>Rs. ${ele.price}</b>
// <button>ADD TO CART</button>
// </main>
// `
// })
// })
// })
let fetchData = async ()=>{
let data = await fetch("https://fakestoreapi.com/products")
let finalData = await data.json()
let container = document.getElementById("card_container")
finalData.forEach((ele)=>{
container.innerHTML+=`
<main class="card">
<img src="${ele.image}">
<h1>${ele.title}</h1>
<p>Description: ${ele.description.slice(0,100)}</p>
<b>Rs. ${ele.price}</b>
<button onclick="addToCart('${ele.title}',${ele.price},'${ele.image}')">ADD TO CART</button>
</main>
`
})
}
fetchData()
let count=0;
let cart=[]
let cartValue = document.getElementById("cartValue")
let empty_cart = document.getElementById("empty_cart")
let cartPrice = []
let cart_price = document.getElementById("cart_price")
let addToCart = (productTitle,productPrice,productImg)=>{
count++;
cartValue.innerText=count
let finalObj={
name:productTitle,
price:productPrice,
img:productImg
}
cart.push(finalObj)
// console.log(cart)
let cart_items = document.getElementById("cart_items")
cart_items.innerHTML+=`
<h1>${cart[cart.length-1].name}</h1>
<img src="${cart[cart.length-1].img}" id="cartImage">
<h2>Rs. ${cart[cart.length-1].price}</h2>
`
empty_cart.style.display="none"
cartPrice.push(cart[cart.length-1].price)
let finalPrice = cartPrice.reduce((storage,ele)=>{
return storage+=ele
})
cart_price.innerHTML=`
<p>Your Total Price is: ${finalPrice}</p>
`
}