-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
212 lines (177 loc) · 5.42 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Variables
let displayValue = '';
let a;
let b;
let operator = '';
// Status paragraph
const statusText = document.getElementById('status');
// Arithmetic functions
const add = function(a, b) {
return (Math.round((a + b) * 100000000) / 100000000);
};
const substruct = function(a, b) {
return (Math.round((a - b) * 100000000) / 100000000);
};
const multiply = function(a, b) {
return (Math.round((a * b) * 100000000) / 100000000);
};
const divide = function(a, b) {
if (b === 0) {
statusText.innerHTML = `You can't divide by 0. Push C-button to proceed.`;
return '<~INFINITY~>';
} else {
return (Math.round(a / b * 100000000) / 100000000);
}
};
// Choose operation
const operate = function(operator, a, b) {
if (operator === '+') {
return add(a, b);
} else if (operator === '-') {
return substruct(a, b);
} else if (operator === '*') {
return multiply(a, b);
} else if (operator === '/') {
return divide(a, b);
} else return "Empty Operator";
};
// Store 1st operand + operator
let storeOperator = function(item) {
if (operator === '') {
// Case initial(first) operation - we don't have first operand
if (displayValue !== '') {
a = +displayValue;
}
operator = item.target.innerText;
displayValue = '';
// Chain evaluation
} else {
if (displayValue !== '') {
b = +displayValue;
a = displayValue = operate(operator, a, b);
operator = item.target.innerText;
document.getElementById('display').innerText = displayValue;
displayValue = '';
}
}
};
// Same for keyboard
let storeOperatorByKey = function(item) {
if (operator === '') {
// if (a === undefined || a === null) {
if (displayValue !== '') {
a = +displayValue;
}
operator = item.key;
displayValue = '';
// } else {
// operator = item.key;
// displayValue = '';
// }
} else {
if (displayValue !== '') {
b = +displayValue;
a = displayValue = operate(operator, a, b);
operator = item.key;
document.getElementById('display').innerText = displayValue;
displayValue = '';
}
}
};
// Store 2nd operand and evaluate
const showingResults = function() {
if (a !== undefined && displayValue !== '') {
b = +displayValue;
}
if (b !== undefined && operator !== '') {
a = displayValue = operate(operator, a, b);
document.getElementById('display').innerText = displayValue;
displayValue = '';
operator = '';
}
};
const fillDisplay = function(item) {
// Limit digits input
if (displayValue.length < 10) {
if (item.target.innerText === '.') {
// Add 0 in case starting input with "."
if (displayValue === '') {
displayValue += '0';
displayValue += item.target.innerText;
document.getElementById('display').innerText = displayValue;
} else {
// Limit "." input by only one
if (!displayValue.includes('.'))
displayValue += item.target.innerText;
document.getElementById('display').innerText = displayValue;
}
} else {
displayValue += item.target.innerText;
document.getElementById('display').innerText = displayValue;
}
}
};
// Same for keyboard
let fillDisplayByKeys = function(item) {
if (displayValue.length < 10) {
if (item.key === '.') {
if (displayValue === '') {
displayValue += '0';
displayValue += item.key;
document.getElementById('display').innerText = displayValue;
} else {
if (!displayValue.includes('.'))
displayValue += item.key;
document.getElementById('display').innerText = displayValue;
}
} else {
displayValue += item.key;
document.getElementById('display').innerText = displayValue;
}
}
};
const clearAll = function() {
// TODO:
// The following code works in debagger just fine, but doesn't in browser.
// Evaluation just shows blank screen. Mystery to be solved...
// I'll go with reload page for now.
// displayValue = '';
// operator = '';
// document.getElementById('display').innerText = displayValue;
// statusText.innerHTML = '';
window.location.reload(false);
};
const deleteLastDigit = function() {
if (displayValue !== '') {
displayValue = displayValue.slice(0, -1);
document.getElementById('display').innerText = displayValue;
}
};
// Event listeners
const digitButtons = Array.from(document.getElementsByClassName('digit'));
digitButtons.forEach(item => {
item.addEventListener('click', fillDisplay);
});
const operatorButtons = Array.from(document.getElementsByClassName('operator'));
operatorButtons.forEach(item => {
item.addEventListener('click', storeOperator);
});
const equalsButton = document.getElementById('equals');
equalsButton.addEventListener('click', showingResults);
const clearButton = document.getElementById('clear');
clearButton.addEventListener('click', clearAll);
const bckspaceButton = document.getElementById('bckspace');
bckspaceButton.addEventListener('click', deleteLastDigit);
document.addEventListener('keydown', function(item) {
let keyDigitCheck = /[\d\.]/;
let keyOperatorCheck = /[\+\-\(/)\*]/;
if (keyDigitCheck.exec(item.key)) {
fillDisplayByKeys(item);
} else if (item.key === 'Backspace') {
deleteLastDigit();
} else if (item.key === 'Enter' || item.key === "=") {
showingResults();
} else if (keyOperatorCheck.exec(item.key)) {
storeOperatorByKey(item);
}
})