Skip to content

Commit b5537af

Browse files
committed
feat(JS) stroe following regex /[\/*\-+]/ into a re-usable variable
1 parent d54a988 commit b5537af

File tree

1 file changed

+71
-60
lines changed

1 file changed

+71
-60
lines changed

index.js

Lines changed: 71 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
const btns = document.querySelectorAll("[data-value]");
22
let screen = document.querySelector("[data-screen]");
33
const operators = document.querySelectorAll("[data-operator]");
4+
const operatorRegex = /[\/*\-+]/;
5+
6+
7+
8+
9+
410

511
let data = [];
612

@@ -30,72 +36,15 @@ btns.forEach((btn) => {
3036

3137
convertToPercentage(buttonValue);
3238

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)
6340

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-
9441
});
9542
});
9643

9744
// end of forEach() statement
9845

46+
// functions creations
47+
9948
function convertToPercentage(button) {
10049
if (button === "%") {
10150
screen.innerText = screen.innerText / 100;
@@ -224,3 +173,65 @@ function handleNumberButton(button) {
224173
screen.innerText = data.join("");
225174
}
226175
}
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

Comments
 (0)