|
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 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
4 | 10 |
|
5 | 11 | let data = []; |
6 | 12 |
|
@@ -30,72 +36,15 @@ btns.forEach((btn) => { |
30 | 36 |
|
31 | 37 | convertToPercentage(buttonValue); |
32 | 38 |
|
33 | | - function userClicksOnEqualButton(button) { |
34 | | - if (button === "=") { |
35 | | - try { |
36 | | - const replacedArray = data.map((item) => |
37 | | - item === "x" ? "*" : item === "÷" ? "/" : item |
38 | | - ); |
39 | | - // Check if the expression involves 0/0 |
40 | | - // if (areYouDivindingByZero(replacedArray)) { |
41 | | - // screen.innerText = "You cannot divide by zero. Press AC"; |
42 | | - // } |
43 | | - |
44 | | - if (areYouDividingdZeroByZero(replacedArray)) { |
45 | | - screen.innerText = "0÷0 is an invalid format. Press AC"; |
46 | | - } else { |
47 | | - let result = eval(replacedArray.join("")); |
48 | | - console.log(result); |
49 | | - displayResult(replacedArray, result); |
50 | | - screen.innerText = |
51 | | - result === Infinity |
52 | | - ? "You cannot divide by zero. Press AC" |
53 | | - : result; |
54 | | - // divideByZero(screen, result); |
55 | | - data = []; |
56 | | - data.push(result); |
57 | | - } |
58 | | - } catch (e) { |
59 | | - screen.innerText = `${e.name} press AC`; |
60 | | - } |
61 | | - } |
62 | | - } |
| 39 | + userClicksOnEqualButton(buttonValue) |
63 | 40 |
|
64 | | - function areYouDivindingByZero(array) { |
65 | | - for (let i = 0; i < array.length - 2; i++) { |
66 | | - if ( |
67 | | - !isNaN(Number(array[i])) && |
68 | | - array[i + 1] === "/" && |
69 | | - array[i + 2] === "0" |
70 | | - ) { |
71 | | - return true; |
72 | | - } |
73 | | - } |
74 | | - return false; |
75 | | - } |
76 | | - |
77 | | - function areYouDividingdZeroByZero(array) { |
78 | | - for (let i = 0; i < array.length - 2; i++) { |
79 | | - if (array[i] === "0" && array[i + 1] === "/" && array[i + 2] === "0") { |
80 | | - return true; |
81 | | - } |
82 | | - } |
83 | | - return false; |
84 | | - } |
85 | | - |
86 | | - function displayResult(array, outcome) { |
87 | | - array = []; |
88 | | - array.push(outcome); |
89 | | - } |
90 | | - |
91 | | - |
92 | | - |
93 | | - |
94 | 41 | }); |
95 | 42 | }); |
96 | 43 |
|
97 | 44 | // end of forEach() statement |
98 | 45 |
|
| 46 | +// functions creations |
| 47 | + |
99 | 48 | function convertToPercentage(button) { |
100 | 49 | if (button === "%") { |
101 | 50 | screen.innerText = screen.innerText / 100; |
@@ -224,3 +173,65 @@ function handleNumberButton(button) { |
224 | 173 | screen.innerText = data.join(""); |
225 | 174 | } |
226 | 175 | } |
| 176 | + |
| 177 | + |
| 178 | +////// |
| 179 | + |
| 180 | + |
| 181 | +function userClicksOnEqualButton(button) { |
| 182 | + if (button === "=") { |
| 183 | + try { |
| 184 | + const replacedArray = data.map((item) => |
| 185 | + item === "x" ? "*" : item === "÷" ? "/" : item |
| 186 | + ); |
| 187 | + // Check if the expression involves 0/0 |
| 188 | + // if (areYouDivindingByZero(replacedArray)) { |
| 189 | + // screen.innerText = "You cannot divide by zero. Press AC"; |
| 190 | + // } |
| 191 | + |
| 192 | + if (areYouDividingdZeroByZero(replacedArray)) { |
| 193 | + screen.innerText = "0÷0 is an invalid format. Press AC"; |
| 194 | + } else { |
| 195 | + let result = eval(replacedArray.join("")); |
| 196 | + console.log(result); |
| 197 | + displayResult(replacedArray, result); |
| 198 | + screen.innerText = |
| 199 | + result === Infinity |
| 200 | + ? "You cannot divide by zero. Press AC" |
| 201 | + : result; |
| 202 | + // divideByZero(screen, result); |
| 203 | + data = []; |
| 204 | + data.push(result); |
| 205 | + } |
| 206 | + } catch (e) { |
| 207 | + screen.innerText = `${e.name} press AC`; |
| 208 | + } |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +function areYouDivindingByZero(array) { |
| 213 | + for (let i = 0; i < array.length - 2; i++) { |
| 214 | + if ( |
| 215 | + !isNaN(Number(array[i])) && |
| 216 | + array[i + 1] === "/" && |
| 217 | + array[i + 2] === "0" |
| 218 | + ) { |
| 219 | + return true; |
| 220 | + } |
| 221 | + } |
| 222 | + return false; |
| 223 | +} |
| 224 | + |
| 225 | +function areYouDividingdZeroByZero(array) { |
| 226 | + for (let i = 0; i < array.length - 2; i++) { |
| 227 | + if (array[i] === "0" && array[i + 1] === "/" && array[i + 2] === "0") { |
| 228 | + return true; |
| 229 | + } |
| 230 | + } |
| 231 | + return false; |
| 232 | +} |
| 233 | + |
| 234 | +function displayResult(array, outcome) { |
| 235 | + array = []; |
| 236 | + array.push(outcome); |
| 237 | +} |
0 commit comments