@@ -7,16 +7,6 @@ let data = [];
77btns . forEach ( ( btn ) => {
88 btn . addEventListener ( "click" , function ( e ) {
99 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- // }
2010
2111 if ( buttonValue === "(" ) {
2212 let isOpenparenthesis = true ;
@@ -60,8 +50,12 @@ btns.forEach((btn) => {
6050 if ( data . slice ( - 1 ) [ 0 ] === "." ) {
6151 data . pop ( ) ;
6252 }
63- buttonValue === "*" ? buttonValue = "x" : buttonValue === "/" ? buttonValue = "÷" : buttonValue ;
64-
53+ buttonValue === "*"
54+ ? ( buttonValue = "x" )
55+ : buttonValue === "/"
56+ ? ( buttonValue = "÷" )
57+ : buttonValue ;
58+
6559 data . push ( buttonValue ) ;
6660 screen . innerText = data . join ( "" ) ;
6761 }
@@ -73,42 +67,57 @@ btns.forEach((btn) => {
7367 canUserAddDot ( ) ;
7468 }
7569
76-
7770 if ( buttonValue === "=" ) {
7871 try {
79- const replacedArray = data . map ( ( item ) => ( item === "x" ? "*" : item === "÷" ? "/" : item ) ) ;
72+ const replacedArray = data . map ( ( item ) =>
73+ item === "x" ? "*" : item === "÷" ? "/" : item
74+ ) ;
8075 // 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" ;
8382 } else {
8483 let result = eval ( replacedArray . join ( "" ) ) ;
8584 console . log ( result ) ;
8685 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);
8891 data = [ ] ;
8992 data . push ( result ) ;
9093 }
9194 } catch ( e ) {
9295 screen . innerText = `${ e . name } press AC` ;
9396 }
9497 }
95-
9698
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 ;
101110 }
102111
103- function hasZeroDividedByZero ( array ) {
112+ function areYouDividingdZeroByZero ( array ) {
104113 for ( let i = 0 ; i < array . length - 2 ; i ++ ) {
105114 if ( array [ i ] === "0" && array [ i + 1 ] === "/" && array [ i + 2 ] === "0" ) {
106115 return true ;
107116 }
108117 }
109118 return false ;
110119 }
111-
120+
112121 function displayResult ( array , outcome ) {
113122 array = [ ] ;
114123 array . push ( outcome ) ;
@@ -175,12 +184,10 @@ function toggleSign() {
175184 if ( ! isNaN ( currentValue ) ) {
176185 // If it's a number, toggle its sign
177186 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 ) ) ;
179190 screen . innerText = data . join ( "" ) ;
180191 }
181192 }
182193}
183-
184-
185-
186-
0 commit comments