-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
259 lines (209 loc) · 8 KB
/
index.js
File metadata and controls
259 lines (209 loc) · 8 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
let displayElem = document.getElementById("display");
let historyDisplay = document.getElementById("display-history");
let operatorsList = [];
let decimalCheck = [];
let currentOperator = "";
let lastDigitInserted = "";
let result = "";
const numberBtnList = document.querySelectorAll( 'button[data-number]' );
const allClearBtn = document.getElementById("ac-btn");
const backspaceBtn = document.getElementById("backspace-btn");
const operatorsElementList = document.querySelectorAll('button[data-operator]');
const equals = document.getElementById("equals");
// Store a list of all operators
operatorsElementList.forEach(element => {
const dataOperatorValue = element.getAttribute("data-operator");
operatorsList.push(dataOperatorValue);
});
/////////////// Trigger mouse clicks
// Numbers and Decimal
numberBtnList.forEach(button => {
button.addEventListener("click", function() {
const dataValue = button.getAttribute("data-number");
handleNumAndDecimal(dataValue);
});
});
// Operators
operatorsElementList.forEach(button => {
button.addEventListener("click", function() {
const operator = button.getAttribute("data-operator");
handleOperator(operator);
})
});
// Calculate / Submit
equals.addEventListener("click", function() {
calculate(currentOperator);
});
// Backspace and AC
allClearBtn.addEventListener("click", function() {
clearDisplay();
});
backspaceBtn.addEventListener("click", function() {
handleBackspace();
});
//////////////// Trigger Keyboard presses
document.addEventListener("keydown", function(event) {
let key = event.key;
// Handle if number
if ( ! isNaN(key) ){
handleNumAndDecimal(key);
// Handle if decimal is repeated
} else if ( key === "." ) {
if ( !decimalCheck.includes(".") ){
decimalCheck.push(key);
handleNumAndDecimal(key);
} else {
displayElem.innerHTML = displayElem.innerHTML;
historyDisplay.innerHTML = historyDisplay.innerHTML;
}
} else if ( operatorsList.includes(key) ) {
handleOperator(key);
} else if ( key === "Backspace" || key === "Delete") {
handleBackspace();
} else if ( key === "Enter" ) {
event.preventDefault(); // Prevents the browser triggers any selected element
calculate(currentOperator);
} else {
displayElem.innerHTML = displayElem.innerHTML;
historyDisplay.innerHTML = historyDisplay.innerHTML;
};
});
///////////// Functions
function addDigitToDisplay(pressedButton){
displayElem.innerHTML += pressedButton;
}
// handle numbers and decimal
function handleNumAndDecimal(dataValue){
lastDigitInserted = dataValue;
backspaceBtn.disabled = false;
if(displayElem.innerHTML === "0" && dataValue != "0" &&dataValue != "."){
displayElem.innerHTML = "";
addDigitToDisplay(dataValue);
} else if ( displayElem.innerHTML === "0" && dataValue=== "0" ) {
displayElem.innerHTML = 0;
} else if (displayElem.innerHTML === "0" && dataValue=== "0" && historyDisplay.innerHTML.length === 0){
clearDisplay();
// handle if inserted "." and result is not on screen
} else if ( dataValue === "." && displayElem.innerHTML != historyDisplay.innerHTML) {
addDigitToDisplay(dataValue);
document.getElementById("point").disabled = true;
// handle if inserted "." on top of the result
} else if (dataValue === "." && displayElem.innerHTML === historyDisplay.innerHTML) {
clearDisplay();
displayElem.innerHTML = "";
addDigitToDisplay(dataValue);
// Handle if inserted a number on top of result
} else if (dataValue != "." && displayElem.innerHTML === historyDisplay.innerHTML){
clearDisplay();
displayElem.innerHTML = "";
addDigitToDisplay(dataValue);
// Handle if input goes on top of previous Error
} else if ( displayElem.innerHTML === "Error" || historyDisplay.innerHTML === "Error" ){
clearDisplay();
addDigitToDisplay(dataValue);
} else {
addDigitToDisplay(dataValue);
};
};
// Handle operators
function handleOperator(operator){
// Check if the last input was not an operator
if ( !operatorsList.includes(lastDigitInserted) ){
// If there are already an operation in curse waiting, calculate the first, show result on historic and start new operation on top of the previous result
if (currentOperator){
let preResult = calculate(currentOperator);
currentOperator = operator;
historyDisplay.innerHTML = preResult + operator;
historyDisplay.classList.remove("hidden");
displayElem.innerHTML = 0;
// Handle if an operator is inserted on top of Zero
} else if ( displayElem.innerHTML === "0"){
if ( operator === "-" ) {
displayElem.innerHTML = "";
addDigitToDisplay(operator);
} else {
currentOperator = operator;
historyDisplay.innerHTML = 0 + operator;
historyDisplay.classList.remove("hidden");
document.getElementById("point").disabled = false;
}
} else {
currentOperator = operator;
historyDisplay.innerHTML = displayElem.innerHTML + operator;
historyDisplay.classList.remove("hidden");
displayElem.innerHTML = 0;
document.getElementById("point").disabled = false;
}
// If last input was an operator, replace that operator for the new operator
} else {
historyDisplay.innerHTML = historyDisplay.innerHTML.slice(0, -1) + operator;
currentOperator = operator;
}
lastDigitInserted = operator;
// Reset decimal check
decimalCheck = [];
}
// Calculate / Submit
function calculate(currentOperator){
if ( currentOperator ) {
let parcel = historyDisplay.innerHTML.slice(0, -1); // all digits excluding the last which is the operator
switch (currentOperator) {
case "+":
result = parseFloat(parcel) + parseFloat(displayElem.innerHTML);
break;
case "*":
result = parseFloat(parcel) * parseFloat(displayElem.innerHTML);
break;
case "/":
result = parseFloat(parcel) / parseFloat(displayElem.innerHTML);
break;
case "-":
result = parseFloat(parcel) - parseFloat(displayElem.innerHTML);
break;
default:
result = displayElem.innerHTML;
break;
};
handleResult(result);
return result;
} else {
displayElem.innerHTML = displayElem.innerHTML;
}
};
// Backspace
function handleBackspace(){
if ( displayElem.innerHTML.length > 1 ) {
if ( displayElem.innerHTML.slice(-1) === "."){
document.getElementById("point").disabled = false;
decimalCheck = [];
displayElem.innerHTML = displayElem.innerHTML.slice(0, -1);
} else {
displayElem.innerHTML = displayElem.innerHTML.slice(0, -1);
}
} else {
displayElem.innerHTML = 0;
}
};
// Clear Display
function clearDisplay() {
displayElem.innerHTML = "0";
historyDisplay.innerHTML = "";
document.getElementById("point").disabled = false;
backspaceBtn.disabled = false;
currentOperator = "";
decimalCheck = [];
};
// Handle Result
function handleResult(result){
if ( ! isFinite(result) ){
displayElem.innerHTML = "Error";
historyDisplay.innerHTML = 0;
} else {
displayElem.innerHTML = "";
addDigitToDisplay(result);
historyDisplay.innerHTML = displayElem.innerHTML;
};
currentOperator = "";
document.getElementById("point").disabled = false;
backspaceBtn.disabled = true;
};