-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
97 lines (82 loc) · 1.93 KB
/
app.js
File metadata and controls
97 lines (82 loc) · 1.93 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const cart = [];
const DATA = [
{
items: "bag",
price: 200000000,
qt: 1
},
{
items: "phone",
price: 2070000,
qt: 1
},
{
items: "shoe",
price: 7000000,
qt: 1
},
{
items: "fan",
price: 34000000,
qt: 1
},
];
document.querySelector(".items") && document.querySelector(".items");
if (document.querySelector(".items")) {
document.querySelector(".items").innerHTML = DATA.map(function (val, ind) {
return `
<div class="card">
<p>${val.items}</p>
<p>${val.price}</p>
<button class="add">add to cart</button>
</div>
`;
});
const add = document.querySelectorAll(".add");
for (let i = 0; i < add.length; i++) {
add[i].addEventListener("click", function (e) {
const p1 = e.target.parentNode.children[0].innerHTML;
const p2 = e.target.parentNode.children[1].innerHTML;
const info = DATA.filter((val) => {
return val.items === p1;
});
const doExist = cart.filter((val, ind) => {
if (val === info[0]) {
cart[ind].qt += 1
return val;
}
})
if (doExist.length === 0){
cart.push(info[0]);
}
console.log(cart);
console.log(doExist);
genDta();
});
}
}
if (document.querySelector(".cart")) {
genDta();
}
function genDta() {
let qt = 0;
let tAmount = 0;
cart.forEach(val => {
qt += val.qt;
// for (let i = 0; i < val.qt; i++) {
// tAmount += val.price
tAmount += val.price * val.qt;
// }
})
document.querySelector(".cartNum").innerHTML = `cart ${qt}`
document.querySelector(".cartTotalPrice").innerHTML = `TP ${tAmount}`
document.querySelector(".cart").innerHTML = cart.map(function (val, ind) {
return `
<div class="cart_card">
<p>${val.items}</p>
<p>${val.price}</p>
<p>quantity ${val.qt}</p>
</div>
`
});
}