|
| 1 | +const internetConnectionPrice = 200; |
| 2 | +const phoneLinePrice = 150; |
| 3 | +const motorolaPrice = 800; |
| 4 | +const iPhonePrice = 6000; |
| 5 | +const samsungPrice = 1000; |
| 6 | +const sonyPrice = 900; |
| 7 | +const huaweiPrice = 900; |
| 8 | +let totalPrice = 0; |
| 9 | +let isInternetConnection = false; |
| 10 | +let phoneLines = 0; |
| 11 | +let selectedCellPhones = []; |
| 12 | +let receiptNames = ["Internet connection", "Phone lines", "Motorola G99", "iPhone 99", 'Samsung Galaxy 99', "Sony Xperia 99", "Huawei 99"]; |
| 13 | +let receiptQuantity = [0, 0, 0, 0, 0, 0, 0]; // frequency array |
| 14 | +let sel; |
| 15 | +let selectedLeftItemName; |
| 16 | +let selectedRightItemName; |
| 17 | +let indexChosenCellPhones; |
| 18 | +let opt; |
| 19 | + |
| 20 | +function getTotalPrice () { |
| 21 | + document.getElementById("price").innerHTML = `Total price: ${totalPrice} DKK`; |
| 22 | +} |
| 23 | + |
| 24 | +function getSelectedLeftItem() { // get the name of the selected option from the Left side, return 0 if no option is selected |
| 25 | + sel = document.getElementById("cmbCellPhones"); |
| 26 | + console.log("sel", sel); |
| 27 | + if (sel.options[sel.selectedIndex]) { //if an option is selected |
| 28 | + selectedLeftItemName = sel.options[sel.selectedIndex].text; |
| 29 | + console.log("selectedLeftItemName", selectedLeftItemName); |
| 30 | + return 1; |
| 31 | + } else { |
| 32 | + return 0; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function getSelectedRightItem() { // get the name of the selected option from the Right side, return 0 if no option is selected |
| 37 | + sel = document.getElementById("txtChosenCellPhones"); |
| 38 | + console.log("sel", sel); |
| 39 | + if (sel.options[sel.selectedIndex]) { //if an option is selected |
| 40 | + selectedRightItemName = sel.options[sel.selectedIndex].text; |
| 41 | + indexChosenCellPhones = sel.selectedIndex; |
| 42 | + console.log("selectedRightItemName", selectedRightItemName); |
| 43 | + console.log("indexChosenCellPhones", indexChosenCellPhones); |
| 44 | + return 1; |
| 45 | + } else { |
| 46 | + return 0; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function alertMessage() { |
| 51 | + let message = ''; |
| 52 | + |
| 53 | + if(receiptQuantity[1] === 1) { |
| 54 | + receiptNames[1] = "Phone line"; |
| 55 | + } else if(receiptQuantity[1] > 1) { |
| 56 | + receiptNames[1] = "Phone lines" |
| 57 | + } |
| 58 | + |
| 59 | + for (let i = 0; i < receiptQuantity.length; i++){ |
| 60 | + if (receiptQuantity[i] > 0) { |
| 61 | + message = message + ' \u2022 ' + receiptQuantity[i] + 'x ' + receiptNames[i] + '\n'; |
| 62 | + } |
| 63 | + } |
| 64 | + message = message + 'Total price: ' + totalPrice + '\n'; |
| 65 | + if (totalPrice !== 0) { |
| 66 | + return 'You have selected: \n' + message; |
| 67 | + } else { |
| 68 | + return "Nothing is selected! Please select something"; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +document.getElementById("chkInternetConnection").addEventListener("click", () => { |
| 73 | + if (isInternetConnection) { |
| 74 | + isInternetConnection = false; |
| 75 | + totalPrice = totalPrice - internetConnectionPrice; |
| 76 | + receiptQuantity[0] = 0; // isInternetConnection is set to 0 for the receipt |
| 77 | + } else { |
| 78 | + isInternetConnection = true; |
| 79 | + totalPrice = totalPrice + internetConnectionPrice; |
| 80 | + receiptQuantity[0] = 1; // isInternetConnection is set to 1 for the receipt |
| 81 | + } |
| 82 | + console.log("isInternetConnection: ", isInternetConnection); |
| 83 | + console.log("totalPrice: ", totalPrice); |
| 84 | + getTotalPrice(); |
| 85 | +}); |
| 86 | + |
| 87 | +document.getElementById("txtPhoneLines").addEventListener("input", (e) => { |
| 88 | + // reset the total price to 0 |
| 89 | + totalPrice = totalPrice - phoneLines * phoneLinePrice; |
| 90 | + // regex for digits between 0 and 8 |
| 91 | + const numbers = /^[0-9]+$/; |
| 92 | + // 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 |
| 93 | + if(e.target.value < 0 || !e.target.value.match(numbers)) { |
| 94 | + e.target.value = 0; |
| 95 | + } else if (e.target.value > 8 || e.target.value.toString().length > 1){ |
| 96 | + e.target.value = 8; |
| 97 | + } |
| 98 | + console.log("Phone lines: ", e.target.value); |
| 99 | + phoneLines = e.target.value; |
| 100 | + receiptQuantity[1] = Number(phoneLines); // set the phoneLines quantity for the receipt |
| 101 | + // calculate the new total price |
| 102 | + totalPrice = totalPrice + phoneLines * phoneLinePrice; |
| 103 | + console.log("totalPrice: ", totalPrice); |
| 104 | + getTotalPrice(); |
| 105 | +}); |
| 106 | + |
| 107 | +document.getElementById("rightBtn").addEventListener("click", ()=> { |
| 108 | + console.log(" ------------ rightBtn ------------"); |
| 109 | + if (getSelectedLeftItem() === 0) { // if no element is selected |
| 110 | + return 0; |
| 111 | + } else { |
| 112 | + console.log("selectedCellPhones-: ", selectedCellPhones ); |
| 113 | + if (selectedLeftItemName !== undefined) { |
| 114 | + selectedCellPhones.push(selectedLeftItemName); |
| 115 | + console.log("selectedCellPhones+: ", selectedCellPhones ); |
| 116 | + } |
| 117 | + |
| 118 | + let select = document.getElementById('txtChosenCellPhones'); |
| 119 | + console.log("select.length: ", select.length ); |
| 120 | + |
| 121 | + select.textContent = ''; // Delete the content of the Select element from HTML (all the "Option" children) |
| 122 | + console.log("selectedCellPhones.length: ", selectedCellPhones.length ); |
| 123 | + |
| 124 | + for (let i = 0; i < selectedCellPhones.length; i++) { // insert every element of the Array as an Option tag in HTML |
| 125 | + opt = document.createElement('option'); |
| 126 | + opt.value = selectedCellPhones[i]; |
| 127 | + opt.innerHTML = selectedCellPhones[i]; |
| 128 | + opt.selected = true; |
| 129 | + select.appendChild(opt); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if(selectedLeftItemName === "Motorola G99") { |
| 134 | + totalPrice = totalPrice + motorolaPrice; |
| 135 | + receiptQuantity[2]++; |
| 136 | + } else if(selectedLeftItemName === "iPhone 99") { |
| 137 | + totalPrice = totalPrice + iPhonePrice; |
| 138 | + receiptQuantity[3]++; |
| 139 | + } else if(selectedLeftItemName === "Samsung Galaxy 99") { |
| 140 | + totalPrice = totalPrice + samsungPrice; |
| 141 | + receiptQuantity[4]++; |
| 142 | + } else if(selectedLeftItemName === "Sony Xperia 99") { |
| 143 | + totalPrice = totalPrice + sonyPrice; |
| 144 | + receiptQuantity[5]++; |
| 145 | + } else if(selectedLeftItemName === "Huawei 99") { |
| 146 | + totalPrice = totalPrice + huaweiPrice; |
| 147 | + receiptQuantity[6]++; |
| 148 | + } |
| 149 | + getTotalPrice(); |
| 150 | + console.log("totalPrice: ", totalPrice); |
| 151 | + |
| 152 | + |
| 153 | +}); |
| 154 | + |
| 155 | +document.getElementById("leftBtn").addEventListener("click", ()=> { |
| 156 | + console.log(" ------------ leftBtn ------------"); |
| 157 | + if (getSelectedRightItem() === 0) { // if no element is selected |
| 158 | + return 0; |
| 159 | + } else { |
| 160 | + console.log("selectedCellPhones: ", selectedCellPhones ); |
| 161 | + console.log("indexChosenCellPhones: ", indexChosenCellPhones ); |
| 162 | + if (indexChosenCellPhones > -1) { |
| 163 | + selectedCellPhones.splice(indexChosenCellPhones, 1); // delete the selected element |
| 164 | + } |
| 165 | + console.log("selectedCellPhones-: ", selectedCellPhones ); |
| 166 | + console.log("indexChosenCellPhones: ", indexChosenCellPhones ); |
| 167 | + |
| 168 | + let select = document.getElementById('txtChosenCellPhones'); |
| 169 | + |
| 170 | + select.textContent = ''; // Delete the content of the Select element from HTML (all the "Option" children) |
| 171 | + for (let i = 0; i < selectedCellPhones.length; i++){ // insert the Option elements in HTML |
| 172 | + opt = document.createElement('option'); |
| 173 | + opt.value = selectedCellPhones[i]; |
| 174 | + opt.innerHTML = selectedCellPhones[i]; |
| 175 | + opt.selected = true; |
| 176 | + select.appendChild(opt); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + console.log("sel.value: ", sel.value); |
| 181 | + if(totalPrice > 0) { |
| 182 | + if (selectedRightItemName === "Motorola G99") { |
| 183 | + totalPrice = totalPrice - motorolaPrice; |
| 184 | + receiptQuantity[2]--; |
| 185 | + } else if (selectedRightItemName === "iPhone 99") { |
| 186 | + totalPrice = totalPrice - iPhonePrice; |
| 187 | + receiptQuantity[3]--; |
| 188 | + } else if (selectedRightItemName === "Samsung Galaxy 99") { |
| 189 | + totalPrice = totalPrice - samsungPrice; |
| 190 | + receiptQuantity[4]--; |
| 191 | + } else if (selectedRightItemName === "Sony Xperia 99") { |
| 192 | + totalPrice = totalPrice - sonyPrice; |
| 193 | + receiptQuantity[5]--; |
| 194 | + } else if (selectedRightItemName === "Huawei 99") { |
| 195 | + totalPrice = totalPrice - huaweiPrice; |
| 196 | + receiptQuantity[6]--; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + getTotalPrice(); |
| 201 | + console.log("totalPrice: ", totalPrice); |
| 202 | +}); |
| 203 | + |
| 204 | +document.getElementById("buyBtn").addEventListener("click", () => { |
| 205 | + alert(alertMessage()); |
| 206 | +}); |
0 commit comments