-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
75 lines (66 loc) · 2.18 KB
/
app.js
File metadata and controls
75 lines (66 loc) · 2.18 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
const products = [
{
id: 1,
title: 'Lenovo Yoga',
price: 3000,
},
{
id: 2,
title: 'Acer Aspire',
price: 1800,
},
{
id: 3,
title: 'Dell Vostro',
price: 3400
},
];
let order = [];
function addToBasket(productId) {
let added = false;
// TODO: добавить проверку наличия товара в заказе (при наличии выдать alert, что товар уже в корзине)
order.map(o => {
if (productId === o.id) {
alert('Товар уже добавлен');
added = true;
}
})
// TODO: если товар еще не в корзине, добавить его из массива products
if (!added) {
products.forEach(p => {
if (p.id === productId) {
order = [
...order,
p
]
}
})
}
// Эти строчки не трогаем, они отвечают за переотрисовку страницы
renderCart();
rerenderTotalPrice();
}
function removeFromBasket(productId) {
// TODO: описать логику удаления товара из корзины
order = order.filter(o => o.id !== productId);
// Эти строчки не трогаем, они отвечают за переотрисовку страницы
renderCart();
rerenderTotalPrice();
}
function rerenderTotalPrice() {
// TODO: опишите функционал подсчета общей стоимости заказа
let totalPrice = order.reduce((total, amount) => total + amount.price, 0);
// Не меняйте эту строчку
document.getElementById('total').innerText = totalPrice;
}
// Этот метод остается без изменений
function renderCart() {
const cart = document.getElementById('basket-items');
cart.innerHTML = '';
order.forEach(item => {
const el = document.createElement('li');
el.innerText = item.title;
el.onclick = () => removeFromBasket(item.id);
cart.appendChild(el);
})
}