-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.js
77 lines (65 loc) · 1.97 KB
/
order.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
function getPrice(field) {
return field.placeholder || 0;
}
function getQuantity(field) {
return parseFloat(field.value, 10) || 0;
}
function getItemTotal(field) {
return getPrice(field) * getQuantity(field);
}
function hideClass(el) {
el.style.display = "none";
}
function showClass(el) {
el.style.display = "block";
}
function showReset() {
var button = document.getElementById("reset");
button.style = "position: absolute; left: 50%; transform: translateX(-50%);"
}
function resetQuantity(field) {
field.value = ""
}
function reset() {
var field,
button = document.getElementById("reset"),
total = document.getElementById('totalPrice');
for (i = 0; i < items.length; i += 1) {
item = items[i];
field = form.elements[item + 'qty'];
resetQuantity(field);
}
total.style.display = "none";
button.style.display = "none";
}
function updateTotal(el, amount) {
hideClass(el);
if (amount > 0) {
showClass(el);
showReset();
el.innerHTML = "Your Order Total is $" + amount.toFixed(2);
}
}
function forEachFormItem(form, func) {
var i,
item,
field,
result = 0;
for (i = 0; i < items.length; i += 1) {
item = items[i];
field = form.elements[item + 'qty'];
result += func(field);
}
return result;
}
function calculateTotal() {
var form = this,
total = 0,
priceField = form.priceField;
total = forEachFormItem(form, getItemTotal);
updateTotal(priceField, total);
}
const items = ['walnut', 'horn', 'gcookie', 'hcookie', 'acorn', 'chocprune', 'macaron', 'crunchypuff', 'peach', 'lemon', 'chestnut', 'tower', 'pavlova', 'cakepop', 'minicupcake', 'cheesecake', 'prunecup', 'cupcake', 'tiramisucup', 'tart', 'sccake', 'lccake', 'sscake', 'lscake', 'sdcake'];
const form = document.getElementById('order');
form.priceField = document.getElementById('totalPrice');
form.addEventListener('change', calculateTotal);