-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
206 lines (185 loc) · 7.85 KB
/
index.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const internetConnectionPrice = 200;
const phoneLinePrice = 150;
const motorolaPrice = 800;
const iPhonePrice = 6000;
const samsungPrice = 1000;
const sonyPrice = 900;
const huaweiPrice = 900;
let totalPrice = 0;
let isInternetConnection = false;
let phoneLines = 0;
let selectedCellPhones = [];
let receiptNames = ["Internet connection", "Phone lines", "Motorola G99", "iPhone 99", 'Samsung Galaxy 99', "Sony Xperia 99", "Huawei 99"];
let receiptQuantity = [0, 0, 0, 0, 0, 0, 0]; // frequency array
let sel;
let selectedLeftItemName;
let selectedRightItemName;
let indexChosenCellPhones;
let opt;
function getTotalPrice () {
document.getElementById("price").innerHTML = `Total price: ${totalPrice} DKK`;
}
function getSelectedLeftItem() { // get the name of the selected option from the Left side, return 0 if no option is selected
sel = document.getElementById("cmbCellPhones");
console.log("sel", sel);
if (sel.options[sel.selectedIndex]) { //if an option is selected
selectedLeftItemName = sel.options[sel.selectedIndex].text;
console.log("selectedLeftItemName", selectedLeftItemName);
return 1;
} else {
return 0;
}
}
function getSelectedRightItem() { // get the name of the selected option from the Right side, return 0 if no option is selected
sel = document.getElementById("txtChosenCellPhones");
console.log("sel", sel);
if (sel.options[sel.selectedIndex]) { //if an option is selected
selectedRightItemName = sel.options[sel.selectedIndex].text;
indexChosenCellPhones = sel.selectedIndex;
console.log("selectedRightItemName", selectedRightItemName);
console.log("indexChosenCellPhones", indexChosenCellPhones);
return 1;
} else {
return 0;
}
}
function alertMessage() {
let message = '';
if(receiptQuantity[1] === 1) {
receiptNames[1] = "Phone line";
} else if(receiptQuantity[1] > 1) {
receiptNames[1] = "Phone lines"
}
for (let i = 0; i < receiptQuantity.length; i++){
if (receiptQuantity[i] > 0) {
message = message + ' \u2022 ' + receiptQuantity[i] + 'x ' + receiptNames[i] + '\n';
}
}
message = message + 'Total price: ' + totalPrice + '\n';
if (totalPrice !== 0) {
return 'You have selected: \n' + message;
} else {
return "Nothing is selected! Please select something";
}
}
document.getElementById("chkInternetConnection").addEventListener("click", () => {
if (isInternetConnection) {
isInternetConnection = false;
totalPrice = totalPrice - internetConnectionPrice;
receiptQuantity[0] = 0; // isInternetConnection is set to 0 for the receipt
} else {
isInternetConnection = true;
totalPrice = totalPrice + internetConnectionPrice;
receiptQuantity[0] = 1; // isInternetConnection is set to 1 for the receipt
}
console.log("isInternetConnection: ", isInternetConnection);
console.log("totalPrice: ", totalPrice);
getTotalPrice();
});
document.getElementById("txtPhoneLines").addEventListener("input", (e) => {
// reset the total price to 0
totalPrice = totalPrice - phoneLines * phoneLinePrice;
// regex for digits between 0 and 8
const numbers = /^[0-9]+$/;
// if the input field is less than 0, is not a number or the length is higher than 1, then the input field is reset to 0
if(e.target.value < 0 || !e.target.value.match(numbers)) {
e.target.value = 0;
} else if (e.target.value > 8 || e.target.value.toString().length > 1){
e.target.value = 8;
}
console.log("Phone lines: ", e.target.value);
phoneLines = e.target.value;
receiptQuantity[1] = Number(phoneLines); // set the phoneLines quantity for the receipt
// calculate the new total price
totalPrice = totalPrice + phoneLines * phoneLinePrice;
console.log("totalPrice: ", totalPrice);
getTotalPrice();
});
document.getElementById("rightBtn").addEventListener("click", ()=> {
console.log(" ------------ rightBtn ------------");
if (getSelectedLeftItem() === 0) { // if no element is selected
return 0;
} else {
console.log("selectedCellPhones-: ", selectedCellPhones );
if (selectedLeftItemName !== undefined) {
selectedCellPhones.push(selectedLeftItemName);
console.log("selectedCellPhones+: ", selectedCellPhones );
}
let select = document.getElementById('txtChosenCellPhones');
console.log("select.length: ", select.length );
select.textContent = ''; // Delete the content of the Select element from HTML (all the "Option" children)
console.log("selectedCellPhones.length: ", selectedCellPhones.length );
for (let i = 0; i < selectedCellPhones.length; i++) { // insert every element of the Array as an Option tag in HTML
opt = document.createElement('option');
opt.value = selectedCellPhones[i];
opt.innerHTML = selectedCellPhones[i];
opt.selected = true;
select.appendChild(opt);
}
}
if(selectedLeftItemName === "Motorola G99") {
totalPrice = totalPrice + motorolaPrice;
receiptQuantity[2]++;
} else if(selectedLeftItemName === "iPhone 99") {
totalPrice = totalPrice + iPhonePrice;
receiptQuantity[3]++;
} else if(selectedLeftItemName === "Samsung Galaxy 99") {
totalPrice = totalPrice + samsungPrice;
receiptQuantity[4]++;
} else if(selectedLeftItemName === "Sony Xperia 99") {
totalPrice = totalPrice + sonyPrice;
receiptQuantity[5]++;
} else if(selectedLeftItemName === "Huawei 99") {
totalPrice = totalPrice + huaweiPrice;
receiptQuantity[6]++;
}
getTotalPrice();
console.log("totalPrice: ", totalPrice);
});
document.getElementById("leftBtn").addEventListener("click", ()=> {
console.log(" ------------ leftBtn ------------");
if (getSelectedRightItem() === 0) { // if no element is selected
return 0;
} else {
console.log("selectedCellPhones: ", selectedCellPhones );
console.log("indexChosenCellPhones: ", indexChosenCellPhones );
if (indexChosenCellPhones > -1) {
selectedCellPhones.splice(indexChosenCellPhones, 1); // delete the selected element
}
console.log("selectedCellPhones-: ", selectedCellPhones );
console.log("indexChosenCellPhones: ", indexChosenCellPhones );
let select = document.getElementById('txtChosenCellPhones');
select.textContent = ''; // Delete the content of the Select element from HTML (all the "Option" children)
for (let i = 0; i < selectedCellPhones.length; i++){ // insert the Option elements in HTML
opt = document.createElement('option');
opt.value = selectedCellPhones[i];
opt.innerHTML = selectedCellPhones[i];
opt.selected = true;
select.appendChild(opt);
}
}
console.log("sel.value: ", sel.value);
if(totalPrice > 0) {
if (selectedRightItemName === "Motorola G99") {
totalPrice = totalPrice - motorolaPrice;
receiptQuantity[2]--;
} else if (selectedRightItemName === "iPhone 99") {
totalPrice = totalPrice - iPhonePrice;
receiptQuantity[3]--;
} else if (selectedRightItemName === "Samsung Galaxy 99") {
totalPrice = totalPrice - samsungPrice;
receiptQuantity[4]--;
} else if (selectedRightItemName === "Sony Xperia 99") {
totalPrice = totalPrice - sonyPrice;
receiptQuantity[5]--;
} else if (selectedRightItemName === "Huawei 99") {
totalPrice = totalPrice - huaweiPrice;
receiptQuantity[6]--;
}
}
getTotalPrice();
console.log("totalPrice: ", totalPrice);
});
document.getElementById("buyBtn").addEventListener("click", () => {
alert(alertMessage());
});