7
7
var op = '+' , result = 0 , isShowResult = true ;
8
8
9
9
const KEY = 'keydown' , CLICK = 'click' , MAX_CHARS = 23 ;
10
- const ZERO = '0' , DOT = '.' , NEG = '-' ;
10
+ const ZERO = '0' , DOT = '.' , NEG = '-' , EMPTY = '' ;
11
11
12
12
const calc = /** @type {HTMLDivElement } */
13
13
( document . getElementById ( 'calculator' ) ) ;
@@ -32,7 +32,8 @@ calc.addEventListener(KEY, btnKeyEvent);
32
32
*
33
33
* @description Firstly it checks if backspace key was pressed.
34
34
* If so, it invokes function deleteLastChar() to delete previous char entered.
35
- * Secondly it checks if delete key was pressed and invokes btnClearEntryEvent()
35
+ * Secondly it checks if Insert key was pressed and invokes btnNegateEvent().
36
+ * Thirdly it checks if delete key was pressed and invokes btnClearEntryEvent()
36
37
* if that's the case, thus behaving the same as the CE's #ce `button`.
37
38
* Then it checks if the event is not a 'repeat'.
38
39
* If it's not a 'repeat', it iterates over each .key `button`.
@@ -43,6 +44,8 @@ calc.addEventListener(KEY, btnKeyEvent);
43
44
function btnKeyEvent ( { key, keyCode, repeat } ) {
44
45
if ( keyCode == 8 ) deleteLastChar ( ) ; // key: "Backspace" (ASCII 8)
45
46
47
+ else if ( keyCode == 45 ) btnNegateEvent ( ) ; // key: "Insert" (ASCII 45)
48
+
46
49
else if ( keyCode == 46 ) btnClearEntryEvent ( ) ; // key: "Delete" (ASCII 46)
47
50
48
51
else if ( ! repeat ) for ( const btn of keyButtons ) if ( key == btn . textContent ) {
@@ -215,3 +218,59 @@ function btnNegateEvent() {
215
218
}
216
219
217
220
// ========================================================================== \\
221
+
222
+ document . getElementById ( 'sqrt' ) ?. addEventListener ( CLICK , btnSqrtEvent ) ;
223
+
224
+ /**
225
+ * Handles the 'click' event of the #sqrt `button` element.
226
+ * Calculates square root of the current value on the #calculator's #display.
227
+ *
228
+ * @description This callback triggers when #sqrt `button` is clicked.
229
+ * If `isShowResult` is true, it calculates the square root of the result.
230
+ * Then it calculates and displays the square root of the current display value.
231
+ */
232
+ function btnSqrtEvent ( ) {
233
+ if ( isShowResult ) result = Math . sqrt ( result ) || 0 ;
234
+ display . textContent = ( Math . sqrt ( + display . innerText ) || 0 ) + EMPTY ;
235
+ }
236
+
237
+ // ========================================================================== \\
238
+
239
+ document . getElementById ( 'percent' ) ?. addEventListener ( CLICK , btnPercentEvent ) ;
240
+
241
+ /**
242
+ * Handles the 'click' event of the #percent `button` element.
243
+ * Calculates percentage of the current value on the #calculator's #display
244
+ * relative to the result.
245
+ *
246
+ * @description This callback triggers when #percent `button` is clicked.
247
+ * If `isShowResult` is true, it does nothing.
248
+ * Otherwise, it calculates and displays the percentage of the current display
249
+ * value relative to the result.
250
+ */
251
+ function btnPercentEvent ( ) {
252
+ if ( isShowResult ) return ;
253
+ display . textContent = + display . innerText * result / 100 + EMPTY ;
254
+ }
255
+
256
+ // ========================================================================== \\
257
+
258
+ document . getElementById ( 'reciprocal' ) ?. addEventListener ( CLICK , btnInverseEvent ) ;
259
+
260
+ /**
261
+ * Handles the 'click' event of the #reciprocal `button` element.
262
+ * Calculates reciprocal (1/x) of current value on the #calculator's #display.
263
+ *
264
+ * @description This callback triggers when #reciprocal `button` is clicked.
265
+ * If `isShowResult` is true, it calculates the reciprocal of the result.
266
+ * Then it calculates and displays the reciprocal of the current display value.
267
+ */
268
+ function btnInverseEvent ( ) {
269
+ var r ;
270
+
271
+ if ( isShowResult ) result = isFinite ( r = 1 / result ) ? r : 0 ;
272
+
273
+ display . textContent = ( isFinite ( r = 1 / + display . innerText ) ? r : 0 ) + EMPTY ;
274
+ }
275
+
276
+ // ========================================================================== \\
0 commit comments