-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
120 lines (104 loc) · 3.58 KB
/
script.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
let basket_food = []
let basket_prices = []
let basket_amount = []
let foods = [
'Pizza Salami',
'Pizza Schinken',
'Pizza funghi',
'Lasagne al forno',
'Spaghetti Bolognese'
]
let prices = [9.5, 9.0, 9.8, 10.0, 11.5]
function render () {
let content = document.getElementById('content')
content.innerHTML = ''
for (let i = 0; i < foods.length; i++) {
const food = foods[i]
const price = prices[i]
content.innerHTML += `
<div class="food">
<div class="foodtext">
<h4 id="name[i]">${food}</h4>
<p id="price[i]" class="price">${price.toFixed(2).replace(".", ",")}€</p>
</div>
<button onclick="addItem(${i}); renderShoppingcart();" class="addbutton">+</button>
</div>`
}
}
function addItem (i) {
let name = foods[i]
let price = prices[i]
let index = basket_food.indexOf(name)
if (index == -1) {
basket_food.push(name)
basket_prices.push(price)
basket_amount.push(1)
} else {
basket_amount[index]++
}
}
function renderShoppingcart () {
let cartcontent = document.getElementById('cartcontent')
let cartcontentoverlay = document.getElementById('cartcontentoverlay')
cartcontent.innerHTML = '';
cartcontentoverlay.innerHTML = '';
for (let index = 0; index < basket_food.length; index++) {
const contentFood = basket_food[index]
const contentPrice = basket_prices[index]
const contentAmount = basket_amount[index]
cartcontent.innerHTML += `
<div class= "generatedcartcontent">
<p> ${contentAmount}x</p><p>${contentFood}</p><p>${(contentPrice * contentAmount).toFixed(2).replace(".", ",")} €</p><img class="trashicon" onclick="deleteItem(${index})" src="img/trash-9-64.png">
</div>
<div class="buttonalignment"><button onclick="minusOne(${index})" class="buttoncart">-</button><button onclick="plusOne(${index})" class="buttoncart">+</button></div>`
cartcontentoverlay.innerHTML += `
<div class= "generatedcartcontent">
<p> ${contentAmount}x</p><p>${contentFood}</p><p>${(contentPrice * contentAmount).toFixed(2).replace(".", ",")} €</p><img class="trashicon" onclick="deleteItem(${index})" src="img/trash-9-64.png">
</div>
<div class="buttonalignment"><button onclick="minusOne(${index})" class="buttoncart">-</button><button onclick="plusOne(${index})" class="buttoncart">+</button></div>`
}
sum()
countCart()
}
function sum () {
document.getElementById('totalsum').innerHTML = ``;
document.getElementById('totalsumoverlay').innerHTML = ``;
let sum = 0
for (let i = 0; i < basket_prices.length; i++) {
sum += (basket_prices[i] * basket_amount[i])
document.getElementById('totalsum').innerHTML = `${sum.toFixed(2).replace(".", ",")} €`;
document.getElementById('totalsumoverlay').innerHTML = `${sum.toFixed(2).replace(".", ",")} €`;
}
}
function countCart(){
document.getElementById('countCart').innerHTML = ``;
let sumCart = 0
for (let i = 0; i < basket_amount.length; i++) {
sumCart += basket_amount[i]
document.getElementById('countCart').innerHTML = `${sumCart}`;
}
}
function plusOne (i) {
basket_amount[i]++
renderShoppingcart()
}
function minusOne (i) {
if (basket_amount[i] > 1) {
basket_amount[i]--
} else {
deleteItem()
}
renderShoppingcart()
}
function deleteItem (index) {
basket_food.splice(index, 1)
basket_prices.splice(index, 1)
basket_amount.splice(index, 1)
renderShoppingcart()
}
function openPopUp () {
document.getElementById('overlay-menu').classList.add('show-overlay-menu')
}
function closePopUp () {
document.getElementById('overlay-menu').classList.remove('show-overlay-menu')
}