@@ -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,22 +67,26 @@ 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
8176 // if (areYouDivindingByZero(replacedArray)) {
8277 // screen.innerText = "You cannot divide by zero. Press AC";
8378 // }
84-
79+
8580 if ( areYouDividingdZeroByZero ( replacedArray ) ) {
8681 screen . innerText = "0÷0 is an invalid format. Press AC" ;
87- } else {
82+ } else {
8883 let result = eval ( replacedArray . join ( "" ) ) ;
89- console . log ( result )
84+ console . log ( result ) ;
9085 displayResult ( replacedArray , result ) ;
91- screen . innerText = result === Infinity ? "You cannot divide by zero. Press AC" : result
86+ screen . innerText =
87+ result === Infinity
88+ ? "You cannot divide by zero. Press AC"
89+ : result ;
9290 // divideByZero(screen, result);
9391 data = [ ] ;
9492 data . push ( result ) ;
@@ -97,21 +95,19 @@ btns.forEach((btn) => {
9795 screen . innerText = `${ e . name } press AC` ;
9896 }
9997 }
100-
10198
10299 function areYouDivindingByZero ( array ) {
103100 for ( let i = 0 ; i < array . length - 2 ; i ++ ) {
104- if ( ! isNaN ( Number ( array [ i ] ) ) && array [ i + 1 ] === "/" && array [ i + 2 ] === "0" ) {
101+ if (
102+ ! isNaN ( Number ( array [ i ] ) ) &&
103+ array [ i + 1 ] === "/" &&
104+ array [ i + 2 ] === "0"
105+ ) {
105106 return true ;
106107 }
107108 }
108109 return false ;
109110 }
110-
111-
112-
113-
114-
115111
116112 function areYouDividingdZeroByZero ( array ) {
117113 for ( let i = 0 ; i < array . length - 2 ; i ++ ) {
@@ -121,7 +117,7 @@ btns.forEach((btn) => {
121117 }
122118 return false ;
123119 }
124-
120+
125121 function displayResult ( array , outcome ) {
126122 array = [ ] ;
127123 array . push ( outcome ) ;
@@ -188,12 +184,10 @@ function toggleSign() {
188184 if ( ! isNaN ( currentValue ) ) {
189185 // If it's a number, toggle its sign
190186 currentValue = - currentValue ;
191- 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 ) ) ;
192190 screen . innerText = data . join ( "" ) ;
193191 }
194192 }
195193}
196-
197-
198-
199-
0 commit comments