@@ -3,10 +3,15 @@ const historyElement = document.querySelector(".computation-history");
33let screen = document . querySelector ( "[data-screen]" ) ;
44const historyBtn = document . querySelector ( ".history-btn" ) ;
55const slidingPart = document . querySelector ( ".sliding-part" ) ;
6- const computationHistoryParent = document . querySelector ( ".computation-history-parent" ) ;
6+ const computationHistoryParent = document . querySelector (
7+ ".computation-history-parent"
8+ ) ;
79const operators = document . querySelectorAll ( "[data-operator]" ) ;
810const clearHistoryBtn = document . querySelector ( ".clear-history-btn" ) ;
9- let currentExpression
11+ const minus = document . querySelector ( ".minus" ) ;
12+ console . log ( minus ) ;
13+
14+ let currentExpression ;
1015
1116clearHistoryBtn . addEventListener ( "click" , ( ) => {
1217 historyElement . innerHTML = "" ;
@@ -84,9 +89,10 @@ btns.forEach((btn) => {
8489// forEach ends & functions creations begins
8590function convertToPercentage ( button ) {
8691 if ( button === "%" ) {
87- currentExpression = data . join ( '' ) ;
92+ currentExpression = data . join ( "" ) ;
8893 currentExpression = currentExpression / 100 ;
8994 data = [ currentExpression ] ;
95+ console . log ( data ) ;
9096 screen . innerText = currentExpression ;
9197 }
9298}
@@ -133,28 +139,67 @@ function deleteEverythingFromScreen(button) {
133139 }
134140}
135141
142+ // function toggleSign(button) {
143+ // if (button === "minus") {
144+ // console.log(data[0])
145+ // currentExpression = data.join("");
146+ // console.log(currentExpression)
147+ // let reversedExpression = currentExpression.split("").join("");
148+ // console.log(reversedExpression)
149+ // let match = reversedExpression.match(/(\d+(\.\d+)?)|(\D+)/); // Match a number or non-digit
150+
151+ // if (match) {
152+ // let start = currentExpression.length - match[ZERO].length;
153+ // let end = currentExpression.length;
154+ // let currentValue = Number(match[ZERO]);
155+
156+ // if (!isNaN(currentValue)) {
157+ // currentValue = -currentValue;
158+ // data = data
159+ // .slice(ZERO, start)
160+ // .concat(currentValue.toString().split(""), data.slice(end));
161+ // screen.innerText = data.join("");
162+ // }
163+ // }
164+ // }
165+ // }
166+
167+ // The unshift() method of Array instances adds the specified elements to the beginning
168+ // of an array and returns the new length of the array.
169+
170+ // The shift() method of Array instances removes the first element from an array and returns
171+ // that removed element. This method changes the length of the array.
172+
173+ minus . addEventListener ( "click" , toggleSign ) ;
174+
175+ // function toggleSign(button) {
176+ // let value = Number(data.join(""))
177+ // if (button === "minus") {
178+ // if (value > 0) {
179+ // value = -value
180+ // console.log(value);
181+ // screen.innerText = value;
182+ // } if (value < 0) {
183+ // console.log("smaller than zero")
184+ // value = value * -1
185+ // console.log(value)
186+ // screen.innerText = value;
187+ // }
188+ // // screen.innerText = value;
189+ // data = [value]
190+ // }
191+ // }
192+
136193function toggleSign ( button ) {
194+ let value = Number ( data . join ( "" ) ) ;
137195 if ( button === "minus" ) {
138- console . log ( data )
139- currentExpression = data . join ( "" ) ;
140- console . log ( currentExpression )
141- let reversedExpression = currentExpression . split ( "" ) . join ( "" ) ;
142- console . log ( reversedExpression )
143- let match = reversedExpression . match ( / ( \d + ( \. \d + ) ? ) | ( \D + ) / ) ; // Match a number or non-digit
144-
145- if ( match ) {
146- let start = currentExpression . length - match [ ZERO ] . length ;
147- let end = currentExpression . length ;
148- let currentValue = Number ( match [ ZERO ] ) ;
149-
150- if ( ! isNaN ( currentValue ) ) {
151- currentValue = - currentValue ;
152- data = data
153- . slice ( ZERO , start )
154- . concat ( currentValue . toString ( ) . split ( "" ) , data . slice ( end ) ) ;
155- screen . innerText = data . join ( "" ) ;
156- }
196+ if ( value > 0 ) {
197+ value = - value ;
198+ } else if ( value < 0 ) {
199+ value = value * - 1 ;
157200 }
201+ screen . innerText = value ;
202+ data = [ value ] ;
158203 }
159204}
160205
0 commit comments