Skip to content

Commit d186b03

Browse files
committed
activateEntryState()
1 parent d1a57f0 commit d186b03

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

calculator.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
// ========================================================================== \\
66

7-
var op = '+', result = 0, isShowResult = true;
7+
var op = '+', entry = 0, result = 0, isShowResult = true;
88

99
const KEY = 'keydown', CLICK = 'click', MAX_CHARS = 23;
1010
const ZERO = '0', DOT = '.', NEG = '-', EMPTY = '';
@@ -15,6 +15,14 @@ const calc = /** @type {HTMLDivElement} */
1515
const display = /** @type {HTMLSpanElement} */
1616
(document.getElementById('display'));
1717

18+
/**
19+
* Resets the state back to entry mode.
20+
*/
21+
function activateEntryState() {
22+
isShowResult = false;
23+
result = entry = 0;
24+
}
25+
1826
// ========================================================================== \\
1927

2028
const keyButtons = /** @type {HTMLCollectionOf<HTMLButtonElement>} */
@@ -54,7 +62,7 @@ function btnKeyEvent({ key, keyCode: k, repeat }) {
5462
else if (k == 46) btnClearEntryEvent(); // key: "Delete" (ASCII 46)
5563

5664
else for (const btn of keyButtons) if (key == btn.textContent) {
57-
btn.click(); // for the rest use the key matching .key button's content
65+
btn.click(); // for the rest, use the key matching .key button's content
5866
break; // and then stop the checking loop after a match is found
5967
}
6068
}
@@ -104,7 +112,7 @@ function btnCommonEvent() {
104112
const { innerText: { 0: head, length: len } } = display, { innerText } = this;
105113

106114
if (isShowResult) {
107-
isShowResult = false;
115+
activateEntryState();
108116
display.textContent = innerText;
109117
}
110118

@@ -128,7 +136,7 @@ document.getElementById('zero')?.addEventListener(CLICK, btnZeroEvent);
128136
*/
129137
function btnZeroEvent() {
130138
if (isShowResult) {
131-
isShowResult = false;
139+
activateEntryState();
132140
display.textContent = ZERO;
133141
return;
134142
}
@@ -153,7 +161,7 @@ document.getElementById('decimal')?.addEventListener(CLICK, btnDotEvent);
153161
*/
154162
function btnDotEvent() {
155163
if (isShowResult) {
156-
isShowResult = false;
164+
activateEntryState();
157165
display.textContent = '0.';
158166
return;
159167
}
@@ -177,7 +185,8 @@ document.getElementById('ac')?.addEventListener(CLICK, btnAllClearEvent);
177185
* It also sets the text content of the #display `span` element back to '0'.
178186
*/
179187
function btnAllClearEvent() {
180-
op = '+', result = 0, isShowResult = true;
188+
activateEntryState();
189+
op = '+';
181190
display.textContent = ZERO;
182191
}
183192

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<button class="digit common key" id="two">2</button>
4040
<button class="digit common key" id="three">3</button>
4141
<button class="operator dyadic key" id="minus">-</button>
42-
<button class="operator unary key" id="equals">=</button>
42+
<button class="result key" id="equals">=</button>
4343

4444
<button class="digit key" id="zero">0</button>
4545
<button class="digit key" id="decimal">.</button>

0 commit comments

Comments
 (0)