File tree Expand file tree Collapse file tree 1 file changed +21
-7
lines changed
Expand file tree Collapse file tree 1 file changed +21
-7
lines changed Original file line number Diff line number Diff line change @@ -146,11 +146,25 @@ function deleteEverythingFromScreen() {
146146}
147147
148148function toggleSign ( ) {
149- let currentValue = data [ data . length - 1 ] ;
150- if ( currentValue === undefined ) return ;
151- let toggledValue = - currentValue ;
152- // screen.innerText = toggledValue; // Update the screen
153- // Update the value in the data array
154- data [ data . length - 1 ] = toggledValue ;
155- screen . innerText = data . join ( "" ) ;
149+ let currentExpression = data . join ( "" ) ;
150+ let reversedExpression = currentExpression . split ( "" ) . reverse ( ) . join ( "" ) ;
151+ let match = reversedExpression . match ( / ( \d + ( \. \d + ) ? ) | ( \D + ) / ) ; // Match a number or non-digit
152+ // debugger
153+
154+ if ( match ) {
155+ let start = currentExpression . length - match [ 0 ] . length ;
156+ let end = currentExpression . length ;
157+ let currentValue = Number ( match [ 0 ] ) ;
158+
159+ if ( ! isNaN ( currentValue ) ) {
160+ // If it's a number, toggle its sign
161+ currentValue = - currentValue ;
162+ data = data . slice ( 0 , start ) . concat ( currentValue . toString ( ) . split ( "" ) , data . slice ( end ) ) ;
163+ screen . innerText = data . join ( "" ) ;
164+ }
165+ }
156166}
167+
168+
169+
170+
You can’t perform that action at this time.
0 commit comments