-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
61 lines (48 loc) · 1.52 KB
/
scripts.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
const list = document.querySelector('ul')
const buttonShowAll = document.querySelector('.show-all')
const buttonMapAll = document.querySelector('.map-all')
const sumAll = document.querySelector('.sum-all')
const filterAll = document.querySelector('.filter-all')
function formatCurrency(value) {
const newValue = value.toLocaleString('pt-br', {
style: 'currency',
currency: 'BRL',
})
return newValue
}
function showAll(productsArray) {
let myLi = ''
productsArray.forEach((product) => {
myLi += `
<li>
<img src=${product.src}>
<p>${product.name}</p>
<p class="item-price">R$ ${formatCurrency(product.price)}</p>
</li>
`
})
list.innerHTML = myLi
}
function mapAllItems() {
const newPrices = menuOptions.map((product) => ({
...product,
price: product.price * 0.9,
}))
showAll(newPrices)
}
function sumAllItems() {
const totalValue = menuOptions.reduce((acc, curr) => acc + curr.price, 0)
list.innerHTML = `
<li>
<p>O valor total dos itens é R$ ${formatCurrency(totalValue)}</p>>
</li>
`
}
function filterAllItems() {
const filterJustVegan = menuOptions.filter((product) => product.vegan)
showAll(filterJustVegan)
}
buttonShowAll.addEventListener('click', () => showAll(menuOptions))
buttonMapAll.addEventListener('click', mapAllItems)
sumAll.addEventListener('click', sumAllItems)
filterAll.addEventListener('click', filterAllItems)