|
1 | 1 | const btns = document.querySelectorAll("[data-value]"); |
2 | 2 | let screen = document.querySelector("[data-screen]"); |
3 | 3 | const operators = document.querySelectorAll("[data-operator]"); |
| 4 | +const operatorRegex = /[\/*\-+]/; |
| 5 | +const ZERO = 0; |
| 6 | +const ZERO_DOT = '0.'; |
4 | 7 |
|
5 | 8 | let data = []; |
6 | 9 |
|
7 | 10 | btns.forEach((btn) => { |
8 | 11 | btn.addEventListener("click", function (e) { |
9 | 12 | let buttonValue = e.target.dataset.value; |
10 | 13 |
|
11 | | - if (buttonValue === "(") { |
12 | | - let isOpenparenthesis = true; |
13 | | - for (let i = data.length - 1; i >= 0; i--) { |
14 | | - if (/^\d$/.test(data[i])) { |
15 | | - isOpenparenthesis = false; |
16 | | - break; |
17 | | - } |
18 | | - if (data[i] === ")") { |
19 | | - isOpenparenthesis = false; |
20 | | - break; |
21 | | - } |
22 | | - if (/[\/*\-+]/.test(data[i])) { |
23 | | - break; |
24 | | - } |
25 | | - } |
26 | | - if (isOpenparenthesis) { |
27 | | - data.push("("); |
28 | | - } |
29 | | - screen.innerText = data.join(""); |
30 | | - } |
| 14 | + insertOpeningParenthesis(buttonValue); |
31 | 15 |
|
32 | | - if (buttonValue === ")") { |
33 | | - data.push(")"); |
34 | | - screen.innerText = data.join(""); |
35 | | - } |
| 16 | + insertClosingParenthesis(buttonValue); |
36 | 17 |
|
37 | | - if (buttonValue === "AC") { |
38 | | - deleteEverythingFromScreen(); |
39 | | - } |
| 18 | + deleteEverythingFromScreen(buttonValue); |
40 | 19 |
|
41 | | - if (Number(buttonValue) === 0 && screen.innerText.startsWith("0.")) { |
42 | | - screen.innerText += buttonValue; |
43 | | - } |
44 | | - if (!isNaN(Number(buttonValue))) { |
45 | | - screen.innerText = buttonValue; |
46 | | - data.push(screen.innerText); |
47 | | - screen.innerText = data.join(""); |
| 20 | + toggleSign(buttonValue); |
| 21 | + |
| 22 | + canUserAddDot(buttonValue); |
| 23 | + |
| 24 | + userClicksOnEqualButton(buttonValue); |
| 25 | + |
| 26 | + handlingZeroFollowedByAdecimal(buttonValue); |
| 27 | + |
| 28 | + removesDecimalPointIfPrecededByAnOperator(buttonValue); |
| 29 | + |
| 30 | + handleNumberButton(buttonValue); |
| 31 | + |
| 32 | + deteLastEntry(buttonValue); |
| 33 | + |
| 34 | + convertToPercentage(buttonValue); |
| 35 | + |
| 36 | + |
| 37 | + }); |
| 38 | +}); |
| 39 | +// forEach ends & functions creations begins |
| 40 | +function convertToPercentage(button) { |
| 41 | + if (button === "%") { |
| 42 | + screen.innerText = screen.innerText / 100; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function deteLastEntry(button) { |
| 47 | + if (button === "DE") { |
| 48 | + let newArray = data.slice(ZERO, -1); |
| 49 | + screen.innerText = newArray.join(""); |
| 50 | + data = newArray; |
| 51 | + if (screen.innerText === "") { |
| 52 | + screen.innerText = ZERO; |
48 | 53 | } |
49 | | - if (/[\/*\-+]/.test(buttonValue)) { |
50 | | - if (data.slice(-1)[0] === ".") { |
51 | | - data.pop(); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function canUserAddDot(button) { |
| 58 | + if (button === ".") { |
| 59 | + var dotAllowed = true; |
| 60 | + for (var i = data.length - 1; i >= ZERO; i--) { |
| 61 | + console.log("data > " + data[i]); |
| 62 | + if (data[i] === ".") { |
| 63 | + dotAllowed = false; |
| 64 | + break; |
| 65 | + } else if (operatorRegex.test(data[i])) { |
| 66 | + break; |
52 | 67 | } |
53 | | - buttonValue === "*" |
54 | | - ? (buttonValue = "x") |
55 | | - : buttonValue === "/" |
56 | | - ? (buttonValue = "÷") |
57 | | - : buttonValue; |
58 | | - |
59 | | - data.push(buttonValue); |
60 | | - screen.innerText = data.join(""); |
61 | 68 | } |
62 | | - if (buttonValue === "minus") { |
63 | | - toggleSign(); |
| 69 | + if (dotAllowed) { |
| 70 | + if (data.length == ZERO) { |
| 71 | + data.push("0"); |
| 72 | + } else if (operatorRegex.test(data[data.length - 1])) { |
| 73 | + data.push("0"); |
| 74 | + } |
| 75 | + data.push("."); |
64 | 76 | } |
| 77 | + screen.innerText = data.join(""); |
| 78 | + } |
| 79 | +} |
65 | 80 |
|
66 | | - if (buttonValue === ".") { |
67 | | - canUserAddDot(); |
68 | | - } |
| 81 | +function deleteEverythingFromScreen(button) { |
| 82 | + if (button === "AC") { |
| 83 | + screen.innerText = ""; |
| 84 | + data = []; |
| 85 | + screen.innerText = ZERO; |
| 86 | + } |
| 87 | +} |
69 | 88 |
|
70 | | - if (buttonValue === "=") { |
71 | | - try { |
72 | | - const replacedArray = data.map((item) => |
73 | | - item === "x" ? "*" : item === "÷" ? "/" : item |
74 | | - ); |
75 | | - // Check if the expression involves 0/0 |
76 | | - // if (areYouDivindingByZero(replacedArray)) { |
77 | | - // screen.innerText = "You cannot divide by zero. Press AC"; |
78 | | - // } |
79 | | - |
80 | | - if (areYouDividingdZeroByZero(replacedArray)) { |
81 | | - screen.innerText = "0÷0 is an invalid format. Press AC"; |
82 | | - } else { |
83 | | - let result = eval(replacedArray.join("")); |
84 | | - console.log(result); |
85 | | - displayResult(replacedArray, result); |
86 | | - screen.innerText = |
87 | | - result === Infinity |
88 | | - ? "You cannot divide by zero. Press AC" |
89 | | - : result; |
90 | | - // divideByZero(screen, result); |
91 | | - data = []; |
92 | | - data.push(result); |
93 | | - } |
94 | | - } catch (e) { |
95 | | - screen.innerText = `${e.name} press AC`; |
| 89 | +function toggleSign(button) { |
| 90 | + if (button === "minus") { |
| 91 | + let currentExpression = data.join(""); |
| 92 | + let reversedExpression = currentExpression.split("").join(""); |
| 93 | + let match = reversedExpression.match(/(\d+(\.\d+)?)|(\D+)/); // Match a number or non-digit |
| 94 | + // debugger |
| 95 | + |
| 96 | + if (match) { |
| 97 | + let start = currentExpression.length - match[ZERO].length; |
| 98 | + let end = currentExpression.length; |
| 99 | + let currentValue = Number(match[ZERO]); |
| 100 | + |
| 101 | + if (!isNaN(currentValue)) { |
| 102 | + // If it's a number, toggle its sign |
| 103 | + currentValue = -currentValue; |
| 104 | + data = data |
| 105 | + .slice(ZERO, start) |
| 106 | + .concat(currentValue.toString().split(""), data.slice(end)); |
| 107 | + screen.innerText = data.join(""); |
96 | 108 | } |
97 | 109 | } |
| 110 | + } |
| 111 | +} |
98 | 112 |
|
99 | | - function areYouDivindingByZero(array) { |
100 | | - for (let i = 0; i < array.length - 2; i++) { |
101 | | - if ( |
102 | | - !isNaN(Number(array[i])) && |
103 | | - array[i + 1] === "/" && |
104 | | - array[i + 2] === "0" |
105 | | - ) { |
106 | | - return true; |
107 | | - } |
| 113 | +function insertOpeningParenthesis(button) { |
| 114 | + if (button === "(") { |
| 115 | + let isOpenparenthesis = true; |
| 116 | + for (let i = data.length - 1; i >= ZERO; i--) { |
| 117 | + if (/^\d$/.test(data[i])) { |
| 118 | + isOpenparenthesis = false; |
| 119 | + break; |
108 | 120 | } |
109 | | - return false; |
110 | | - } |
111 | | - |
112 | | - function areYouDividingdZeroByZero(array) { |
113 | | - for (let i = 0; i < array.length - 2; i++) { |
114 | | - if (array[i] === "0" && array[i + 1] === "/" && array[i + 2] === "0") { |
115 | | - return true; |
116 | | - } |
| 121 | + if (data[i] === ")") { |
| 122 | + isOpenparenthesis = false; |
| 123 | + break; |
| 124 | + } |
| 125 | + if (operatorRegex.test(data[i])) { |
| 126 | + break; |
117 | 127 | } |
118 | | - return false; |
119 | 128 | } |
120 | | - |
121 | | - function displayResult(array, outcome) { |
122 | | - array = []; |
123 | | - array.push(outcome); |
| 129 | + if (isOpenparenthesis) { |
| 130 | + data.push("("); |
124 | 131 | } |
| 132 | + screen.innerText = data.join(""); |
| 133 | + } |
| 134 | +} |
125 | 135 |
|
126 | | - if (buttonValue === "%") { |
127 | | - screen.innerText = screen.innerText / 100; |
128 | | - } |
| 136 | +function insertClosingParenthesis(button) { |
| 137 | + if (button === ")") { |
| 138 | + data.push(")"); |
| 139 | + screen.innerText = data.join(""); |
| 140 | + } |
| 141 | +} |
129 | 142 |
|
130 | | - if (buttonValue === "DE") { |
131 | | - deteLastEntry(); |
| 143 | +function handlingZeroFollowedByAdecimal(button) { |
| 144 | + if (Number(button) === ZERO && screen.innerText.startsWith(ZERO_DOT)) { |
| 145 | + screen.innerText += button; |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +function removesDecimalPointIfPrecededByAnOperator(button) { |
| 150 | + if (operatorRegex.test(button)) { |
| 151 | + if (data.slice(-1)[ZERO] === ".") { |
| 152 | + data.pop(); |
132 | 153 | } |
133 | | - }); |
134 | | -}); |
| 154 | + button === "*" ? (button = "x") : button === "/" ? (button = "÷") : button; |
135 | 155 |
|
136 | | -function deteLastEntry() { |
137 | | - let newArray = data.slice(0, -1); |
138 | | - screen.innerText = newArray.join(""); |
139 | | - data = newArray; |
140 | | - if (screen.innerText === "") { |
141 | | - screen.innerText = 0; |
| 156 | + data.push(button); |
| 157 | + screen.innerText = data.join(""); |
142 | 158 | } |
143 | 159 | } |
144 | 160 |
|
145 | | -function canUserAddDot() { |
146 | | - var dotAllowed = true; |
147 | | - for (var i = data.length - 1; i >= 0; i--) { |
148 | | - console.log("data > " + data[i]); |
149 | | - if (data[i] === ".") { |
150 | | - dotAllowed = false; |
151 | | - break; |
152 | | - } else if (/[\/*\-+]/.test(data[i])) { |
153 | | - break; |
154 | | - } |
| 161 | +function handleNumberButton(button) { |
| 162 | + if (!isNaN(Number(button))) { |
| 163 | + screen.innerText = button; |
| 164 | + data.push(screen.innerText); |
| 165 | + screen.innerText = data.join(""); |
155 | 166 | } |
156 | | - if (dotAllowed) { |
157 | | - if (data.length == 0) { |
158 | | - data.push("0"); |
159 | | - } else if (/[\/*\-+]/.test(data[data.length - 1])) { |
160 | | - data.push("0"); |
| 167 | +} |
| 168 | + |
| 169 | +function userClicksOnEqualButton(button) { |
| 170 | + if (button === "=") { |
| 171 | + try { |
| 172 | + const replacedArray = data.map((item) => |
| 173 | + item === "x" ? "*" : item === "÷" ? "/" : item |
| 174 | + ); |
| 175 | + // Check if the expression involves 0/0 |
| 176 | + // if (areYouDivindingByZero(replacedArray)) { |
| 177 | + // screen.innerText = "You cannot divide by zero. Press AC"; |
| 178 | + // } |
| 179 | + |
| 180 | + if (areYouDividingdZeroByZero(replacedArray)) { |
| 181 | + screen.innerText = "0÷0 is an invalid format. Press AC"; |
| 182 | + } else { |
| 183 | + let result = eval(replacedArray.join("")); |
| 184 | + console.log(result); |
| 185 | + displayResult(replacedArray, result); |
| 186 | + screen.innerText = !Number.isFinite(result) ? "You cannot divide by zero. Press AC" : result; |
| 187 | + // divideByZero(screen, result); |
| 188 | + data = []; |
| 189 | + data.push(result); |
| 190 | + } |
| 191 | + } catch (e) { |
| 192 | + screen.innerText = `${e.name} press AC`; |
161 | 193 | } |
162 | | - data.push("."); |
163 | 194 | } |
164 | | - screen.innerText = data.join(" "); |
165 | 195 | } |
166 | 196 |
|
167 | | -function deleteEverythingFromScreen() { |
168 | | - screen.innerText = ""; |
169 | | - data = []; |
170 | | - screen.innerText = 0; |
| 197 | +function areYouDivindingByZero(array) { |
| 198 | + for (let i = ZERO; i < array.length - 2; i++) { |
| 199 | + if ( |
| 200 | + !isNaN(Number(array[i])) && |
| 201 | + array[i + 1] === "/" && |
| 202 | + array[i + 2] === "0" |
| 203 | + ) { |
| 204 | + return true; |
| 205 | + } |
| 206 | + } |
| 207 | + return false; |
171 | 208 | } |
172 | 209 |
|
173 | | -function toggleSign() { |
174 | | - let currentExpression = data.join(""); |
175 | | - let reversedExpression = currentExpression.split("").reverse().join(""); |
176 | | - let match = reversedExpression.match(/(\d+(\.\d+)?)|(\D+)/); // Match a number or non-digit |
177 | | - // debugger |
178 | | - |
179 | | - if (match) { |
180 | | - let start = currentExpression.length - match[0].length; |
181 | | - let end = currentExpression.length; |
182 | | - let currentValue = Number(match[0]); |
183 | | - |
184 | | - if (!isNaN(currentValue)) { |
185 | | - // If it's a number, toggle its sign |
186 | | - currentValue = -currentValue; |
187 | | - data = data |
188 | | - .slice(0, start) |
189 | | - .concat(currentValue.toString().split(""), data.slice(end)); |
190 | | - screen.innerText = data.join(""); |
| 210 | +function areYouDividingdZeroByZero(array) { |
| 211 | + for (let i = ZERO; i < array.length - 2; i++) { |
| 212 | + if (array[i] === "0" && array[i + 1] === "/" && array[i + 2] === "0") { |
| 213 | + return true; |
191 | 214 | } |
192 | 215 | } |
| 216 | + return false; |
| 217 | +} |
| 218 | + |
| 219 | +function displayResult(array, outcome) { |
| 220 | + array = []; |
| 221 | + array.push(outcome); |
193 | 222 | } |
| 223 | +// functions creations ends |
0 commit comments