@@ -7,16 +7,6 @@ let data = [];
7
7
btns . forEach ( ( btn ) => {
8
8
btn . addEventListener ( "click" , function ( e ) {
9
9
let buttonValue = e . target . dataset . value ;
10
- // if (buttonValue === "(") {
11
- // let isOpenParenthesis = true;
12
- // for (let i = data.length - 1; i >= 0; i--) {
13
- // if (/^\d$/.test(data[i])) { // Use /^\d$/ to match a single digit
14
- // isOpenParenthesis = false;
15
- // break; // Exit the loop as soon as a digit is found
16
- // }
17
- // }
18
- // // Rest of your code...
19
- // }
20
10
21
11
if ( buttonValue === "(" ) {
22
12
let isOpenparenthesis = true ;
@@ -60,8 +50,12 @@ btns.forEach((btn) => {
60
50
if ( data . slice ( - 1 ) [ 0 ] === "." ) {
61
51
data . pop ( ) ;
62
52
}
63
- buttonValue === "*" ? buttonValue = "x" : buttonValue === "/" ? buttonValue = "÷" : buttonValue ;
64
-
53
+ buttonValue === "*"
54
+ ? ( buttonValue = "x" )
55
+ : buttonValue === "/"
56
+ ? ( buttonValue = "÷" )
57
+ : buttonValue ;
58
+
65
59
data . push ( buttonValue ) ;
66
60
screen . innerText = data . join ( "" ) ;
67
61
}
@@ -73,42 +67,57 @@ btns.forEach((btn) => {
73
67
canUserAddDot ( ) ;
74
68
}
75
69
76
-
77
70
if ( buttonValue === "=" ) {
78
71
try {
79
- const replacedArray = data . map ( ( item ) => ( item === "x" ? "*" : item === "÷" ? "/" : item ) ) ;
72
+ const replacedArray = data . map ( ( item ) =>
73
+ item === "x" ? "*" : item === "÷" ? "/" : item
74
+ ) ;
80
75
// Check if the expression involves 0/0
81
- if ( hasZeroDividedByZero ( replacedArray ) ) {
82
- screen . innerText = "Invalid format used. You cannot divide by zero" ;
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" ;
83
82
} else {
84
83
let result = eval ( replacedArray . join ( "" ) ) ;
85
84
console . log ( result ) ;
86
85
displayResult ( replacedArray , result ) ;
87
- divideByZero ( screen , result ) ;
86
+ screen . innerText =
87
+ result === Infinity
88
+ ? "You cannot divide by zero. Press AC"
89
+ : result ;
90
+ // divideByZero(screen, result);
88
91
data = [ ] ;
89
92
data . push ( result ) ;
90
93
}
91
94
} catch ( e ) {
92
95
screen . innerText = `${ e . name } press AC` ;
93
96
}
94
97
}
95
-
96
98
97
- function divideByZero ( display , outcome ) {
98
- outcome === Infinity
99
- ? ( display . innerText = "Math Error. Cannot divide by zero" )
100
- : ( display . innerText = outcome ) ;
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
+ }
108
+ }
109
+ return false ;
101
110
}
102
111
103
- function hasZeroDividedByZero ( array ) {
112
+ function areYouDividingdZeroByZero ( array ) {
104
113
for ( let i = 0 ; i < array . length - 2 ; i ++ ) {
105
114
if ( array [ i ] === "0" && array [ i + 1 ] === "/" && array [ i + 2 ] === "0" ) {
106
115
return true ;
107
116
}
108
117
}
109
118
return false ;
110
119
}
111
-
120
+
112
121
function displayResult ( array , outcome ) {
113
122
array = [ ] ;
114
123
array . push ( outcome ) ;
@@ -175,12 +184,10 @@ function toggleSign() {
175
184
if ( ! isNaN ( currentValue ) ) {
176
185
// If it's a number, toggle its sign
177
186
currentValue = - currentValue ;
178
- data = data . slice ( 0 , start ) . concat ( currentValue . toString ( ) . split ( "" ) , data . slice ( end ) ) ;
187
+ data = data
188
+ . slice ( 0 , start )
189
+ . concat ( currentValue . toString ( ) . split ( "" ) , data . slice ( end ) ) ;
179
190
screen . innerText = data . join ( "" ) ;
180
191
}
181
192
}
182
193
}
183
-
184
-
185
-
186
-
0 commit comments