4
4
5
5
// ========================================================================== \\
6
6
7
- var op = '+' , result = 0 , isShowResult = true ;
7
+ var op = '+' , entry = 0 , result = 0 , isShowResult = true ;
8
8
9
9
const KEY = 'keydown' , CLICK = 'click' , MAX_CHARS = 23 ;
10
10
const ZERO = '0' , DOT = '.' , NEG = '-' , EMPTY = '' ;
@@ -15,6 +15,14 @@ const calc = /** @type {HTMLDivElement} */
15
15
const display = /** @type {HTMLSpanElement } */
16
16
( document . getElementById ( 'display' ) ) ;
17
17
18
+ /**
19
+ * Resets the state back to entry mode.
20
+ */
21
+ function activateEntryState ( ) {
22
+ isShowResult = false ;
23
+ result = entry = 0 ;
24
+ }
25
+
18
26
// ========================================================================== \\
19
27
20
28
const keyButtons = /** @type {HTMLCollectionOf<HTMLButtonElement> } */
@@ -54,7 +62,7 @@ function btnKeyEvent({ key, keyCode: k, repeat }) {
54
62
else if ( k == 46 ) btnClearEntryEvent ( ) ; // key: "Delete" (ASCII 46)
55
63
56
64
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
58
66
break ; // and then stop the checking loop after a match is found
59
67
}
60
68
}
@@ -104,7 +112,7 @@ function btnCommonEvent() {
104
112
const { innerText : { 0 : head , length : len } } = display , { innerText } = this ;
105
113
106
114
if ( isShowResult ) {
107
- isShowResult = false ;
115
+ activateEntryState ( ) ;
108
116
display . textContent = innerText ;
109
117
}
110
118
@@ -128,7 +136,7 @@ document.getElementById('zero')?.addEventListener(CLICK, btnZeroEvent);
128
136
*/
129
137
function btnZeroEvent ( ) {
130
138
if ( isShowResult ) {
131
- isShowResult = false ;
139
+ activateEntryState ( ) ;
132
140
display . textContent = ZERO ;
133
141
return ;
134
142
}
@@ -153,7 +161,7 @@ document.getElementById('decimal')?.addEventListener(CLICK, btnDotEvent);
153
161
*/
154
162
function btnDotEvent ( ) {
155
163
if ( isShowResult ) {
156
- isShowResult = false ;
164
+ activateEntryState ( ) ;
157
165
display . textContent = '0.' ;
158
166
return ;
159
167
}
@@ -177,7 +185,8 @@ document.getElementById('ac')?.addEventListener(CLICK, btnAllClearEvent);
177
185
* It also sets the text content of the #display `span` element back to '0'.
178
186
*/
179
187
function btnAllClearEvent ( ) {
180
- op = '+' , result = 0 , isShowResult = true ;
188
+ activateEntryState ( ) ;
189
+ op = '+' ;
181
190
display . textContent = ZERO ;
182
191
}
183
192
0 commit comments