@@ -16,6 +16,10 @@ btns.forEach((btn) => {
1616
1717 toggleSign ( buttonValue ) ;
1818
19+ canUserAddDot ( buttonValue ) ;
20+
21+ userClicksOnEqualButton ( buttonValue ) ;
22+
1923
2024 if ( Number ( buttonValue ) === 0 && screen . innerText . startsWith ( "0." ) ) {
2125 screen . innerText += buttonValue ;
@@ -42,38 +46,38 @@ btns.forEach((btn) => {
4246
4347
4448
49+ function userClicksOnEqualButton ( button ) {
50+ if ( button === "=" ) {
51+ try {
52+ const replacedArray = data . map ( ( item ) => item === "x" ? "*" : item === "÷" ? "/" : item ) ;
53+ // Check if the expression involves 0/0
54+ // if (areYouDivindingByZero(replacedArray)) {
55+ // screen.innerText = "You cannot divide by zero. Press AC";
56+ // }
57+
58+ if ( areYouDividingdZeroByZero ( replacedArray ) ) {
59+ screen . innerText = "0÷0 is an invalid format. Press AC" ;
60+ } else {
61+ let result = eval ( replacedArray . join ( "" ) ) ;
62+ console . log ( result ) ;
63+ displayResult ( replacedArray , result ) ;
64+ screen . innerText =
65+ result === Infinity
66+ ? "You cannot divide by zero. Press AC"
67+ : result ;
68+ // divideByZero(screen, result);
69+ data = [ ] ;
70+ data . push ( result ) ;
71+ }
72+ } catch ( e ) {
73+ screen . innerText = `${ e . name } press AC` ;
74+ }
75+ }
4576
46- canUserAddDot ( buttonValue ) ;
77+ }
4778
4879
49- if ( buttonValue === "=" ) {
50- try {
51- const replacedArray = data . map ( ( item ) =>
52- item === "x" ? "*" : item === "÷" ? "/" : item
53- ) ;
54- // Check if the expression involves 0/0
55- // if (areYouDivindingByZero(replacedArray)) {
56- // screen.innerText = "You cannot divide by zero. Press AC";
57- // }
58-
59- if ( areYouDividingdZeroByZero ( replacedArray ) ) {
60- screen . innerText = "0÷0 is an invalid format. Press AC" ;
61- } else {
62- let result = eval ( replacedArray . join ( "" ) ) ;
63- console . log ( result ) ;
64- displayResult ( replacedArray , result ) ;
65- screen . innerText =
66- result === Infinity
67- ? "You cannot divide by zero. Press AC"
68- : result ;
69- // divideByZero(screen, result);
70- data = [ ] ;
71- data . push ( result ) ;
72- }
73- } catch ( e ) {
74- screen . innerText = `${ e . name } press AC` ;
75- }
76- }
80+
7781
7882 function areYouDivindingByZero ( array ) {
7983 for ( let i = 0 ; i < array . length - 2 ; i ++ ) {
0 commit comments