Skip to content

Commit e8f6d13

Browse files
committed
btnSqrtEvent(),btnPercentEvent(),btnInverseEvent()
1 parent a7fe53c commit e8f6d13

File tree

1 file changed

+61
-2
lines changed

1 file changed

+61
-2
lines changed

calculator.js

+61-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
var op = '+', result = 0, isShowResult = true;
88

99
const KEY = 'keydown', CLICK = 'click', MAX_CHARS = 23;
10-
const ZERO = '0', DOT = '.', NEG = '-';
10+
const ZERO = '0', DOT = '.', NEG = '-', EMPTY = '';
1111

1212
const calc = /** @type {HTMLDivElement} */
1313
(document.getElementById('calculator'));
@@ -32,7 +32,8 @@ calc.addEventListener(KEY, btnKeyEvent);
3232
*
3333
* @description Firstly it checks if backspace key was pressed.
3434
* 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()
3637
* if that's the case, thus behaving the same as the CE's #ce `button`.
3738
* Then it checks if the event is not a 'repeat'.
3839
* If it's not a 'repeat', it iterates over each .key `button`.
@@ -43,6 +44,8 @@ calc.addEventListener(KEY, btnKeyEvent);
4344
function btnKeyEvent({ key, keyCode, repeat }) {
4445
if (keyCode == 8) deleteLastChar(); // key: "Backspace" (ASCII 8)
4546

47+
else if (keyCode == 45) btnNegateEvent(); // key: "Insert" (ASCII 45)
48+
4649
else if (keyCode == 46) btnClearEntryEvent(); // key: "Delete" (ASCII 46)
4750

4851
else if (!repeat) for (const btn of keyButtons) if (key == btn.textContent) {
@@ -215,3 +218,59 @@ function btnNegateEvent() {
215218
}
216219

217220
// ========================================================================== \\
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

Comments
 (0)