-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelperFunctions.js
158 lines (122 loc) · 4.46 KB
/
helperFunctions.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* eslint-disable max-len */
export function validateForm() {
const name = document.forms.card.name.value;
const cardNumber = document.forms.card.cardNumber.value;
const cardCvv = document.forms.card.cardCvv.value;
const missingMsg = 'Please enter your';
if (name === '') {
window.alert(`${missingMsg} name`);
return false;
}
const regex1 = /^[0-9]{13,19}$/;
if (cardNumber === '' || Number.isNaN(cardNumber) || !regex1.test(cardNumber)) {
window.alert(`${missingMsg} valid 13 digit card number`);
return false;
}
const regex2 = /^[0-9]{3,4}$/;
if (cardCvv === '' || Number.isNaN(cardCvv) || !regex2.test(cardCvv)) {
window.alert(`${missingMsg} your 3 or 4 digit card security code located on the back of your card`);
return false;
}
console.log(name);
return true;
}
export function calculateTotalCost(itemArr) {
const totalCost = itemArr.reduce((total, currentItem) => total + currentItem.price, 0);
return totalCost;
}
// Adds up item cost if two items are the same, shows on same line, adding cost to the right, WIP
export function totalItemPrice(ocurrences, addedItem) {
const allItems = [];
let price = {};
addedItem.forEach((item) => {
ocurrences.forEach((key, value) => {
if (key === item.name) {
const totalPrice = value > 0 ? item.price * value : item.price;
price = {};
price = [`"${item.name}": "${totalPrice}"`];
allItems.push(price);
}
});
});
return allItems;
}
// function countItemOcurrences(itemArr){
// // if(itemArr.length === 1){
// // return itemArr;
// // }
// let items = { };
// let count = { };
// let fun = []
// for(let food of itemArr){
// if(count[food.name]){
// // count[food.name] += 1;
// items = {name: food.name, id: food.id, price: food.price, count: count[food.name] += 1}
// } else {
// // count[food.name] = 1;
// items = {name: food.name, id: food.id, price: food.price, count: count[food.name] = 1}
// }
// fun.push(items);
// }
// // console.log(JSON.stringify(items), 'checking for understanding, occurs exist?')
// return fun;
// }
/* Experiment code to refactor app after mvp
// function matchAll() {
// var arr = [
// {"name": "Pizza","ingredients":["pepperoni","mushrom","mozarella"],"price":14,"emoji":"🍕","id":0},
// {"name":"Pizza","ingredients":["pepperoni","mushrom","mozarella"],"price":14,"emoji":"🍕","id":0},
// {"name":"Hamburger","ingredients":["beef","cheese","lettuce"],"price":12,"emoji":"🍔","id":1},{"name":"Beer","ingredients":["grain, hops, yeast, water"],"price":12,"emoji":"🍺","id":2}
// ];
// for(let food of arr){
// const regex = /\b(Pizza|Beer|Hamburger)\b/g
// let res = food.name.match(regex);
// let count = 0;
// switch(res){
// case 'Pizza':
// ;
// case 'Beer':
// case 'Hamburger'
// }
// const foundMatches = res ? res.length : 0;
// console.log(foundMatches)
// }
// }
// var found = matchAll();
// console.log(found);
// function addUp(){
// const foods = [{"name": "pizza", id:1, "price": 5}, {"name": "beer", id:2, "price": 5}, {"name":"hotdog", "id": 3, "price": 3}]
// foods.push({"name": "pizza", id:1, "price": 5})
// foods.push({"name": "beer", id:2, "price": 5})
// foods.push({"name": "beer", id:2, "price": 5})
// // output after counting pizza 2, beer 3, hotdog 1
// // console.log(food)
// const count = {};
// for(let item of foods){
// if(count[item.name]){
// count[item.name] += 1;
// } else{
// count[item.name] = 1;
// }
// }
// console.log(JSON.stringify(count));
// console.log('hello')
// const totalFoodCost = foods.reduce((total, currentItem)=> total + currentItem.price, 0) // total of food calculations
// console.log(totalFoodCost)
// }
// console.log(addUp())
// function displayMessage(item) {
// const displayMsg = document.getElementById("display")
// let message = '';
// const foodName = JSON.stringify(item.name);
// message = `<p>${foodName} has been added to your cart!</p>`
// displayMsg.innerHTML += message;
// // setTimeout(function(){
// // })
// // document.getElementById("alarmmsg").innerHTML = msg;
// setTimeout(function(){
// displayMsg.textContent = '';
// }, 3000); // <--- removes the whole thing
// return message;
// }
*/