-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
233 lines (196 loc) · 5.58 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const DECIMAL_ROUNDING = 6;
/* DOM Element constants */
const displayElement = document.querySelector("#display-txt");
const numButtons = document.querySelectorAll(".num-btn");
const operationButtons = document.querySelectorAll(".op-btn");
const equalsButton = document.querySelector(".eq-btn");
const clearButton = document.querySelector(".ac-btn");
const backspaceButton = document.querySelector(".back-btn");
/*-------------------------*/
let firstNum = NaN;
let secondNum = NaN;
let operator = "";
let displayValue = "";
let waitingForSecondNum = false;
/* The four basic mathematical functions */
function add(a, b) {
return a + b;
}
function sub(a, b) {
return a - b;
}
function mul(a, b) {
return a * b;
}
function div(a, b) {
return a / b;
}
/* ------------------------- */
function operate(a, b, op) {
let result = NaN;
switch (op) {
case "+":
result = add(a, b);
break;
case "-":
result = sub(a, b);
break;
case "*":
result = mul(a, b);
break;
case "/":
if (b === 0) {
alert("You can't divide by zero!");
return NaN;
}
result = div(a, b);
break;
default:
console.error("UNKNOWN OPERATOR: (" + op + ")");
return NaN;
}
// Round the result if it's a number, It has no effect if the number is an integer.
if (result != NaN)
result =
Math.floor(result * Math.pow(10, DECIMAL_ROUNDING)) /
Math.pow(10, DECIMAL_ROUNDING);
return result;
}
// This is Basically the opposite of isNaN, and it also works with strings and numbers.
function isNumber(val) {
return !isNaN(parseFloat(val.toString()));
}
// Update the display to the passed string.
function updateDisplay(str) {
displayValue = str;
displayElement.textContent = displayValue;
}
// This only clears the display.
function clearDisplay() {
updateDisplay("");
}
// This clears and resets everything.
function clearCalculator() {
clearDisplay();
firstNum = NaN;
secondNum = NaN;
operator = "";
waitingForSecondNum = false;
}
// Used by number buttons.
function populateDisplay(str) {
// if the display is NaN, clear the display so that it only contains numbers.
if (!isNumber(displayElement.textContent)) clearDisplay();
if (str === ".") {
// if the input is a floating point and there is already one on the display, return.
if (displayValue.includes(".")) return;
// if display is empty, add a zero before the floating point.
else if (displayValue === "") str = "0.";
}
// add the number that's written in the button to the display.
updateDisplay(displayValue + str);
}
function calculate() {
if (displayValue === "") return;
if (!waitingForSecondNum) return;
secondNum = parseFloat(displayValue);
const result = operate(firstNum, secondNum, operator);
clearCalculator();
updateDisplay(result.toString());
// set the first number to the result so that the user can operate on it,
// if result is NaN then the first number will also be NaN, just like it was
// at the start of the application.
firstNum = result;
}
// triggers on clicking an operator button
function processInput(op) {
// If the input is empty just return
if (displayValue === "") {
// Change the operator even if the display is empty
if (waitingForSecondNum) operator = op;
return;
}
if (waitingForSecondNum) {
calculate();
return;
}
// we don't have the first number so we store it and the operator
firstNum = parseFloat(displayValue);
operator = op;
// we clear the display and wait for second input
clearDisplay();
waitingForSecondNum = true;
}
function backspace() {
if (displayValue === "") return;
let newDisplayValue = displayValue.slice(0, -1);
// Also remove trailing floating point to avoid errors
if (newDisplayValue.endsWith("."))
newDisplayValue = newDisplayValue.slice(0, -1);
updateDisplay(newDisplayValue);
}
function addButtonEvents() {
numButtons.forEach((element) => {
element.addEventListener("click", (_) => {
// Add the num that's written inside the button to the display
populateDisplay(element.textContent);
});
});
operationButtons.forEach((element) => {
element.addEventListener("click", (_) => {
// The operator is the text content of the button (for example "+")
// This should be fine unless the client changes the html of the page,
// but even if he does then the operator will be changed to another one or it will error.
processInput(element.textContent);
});
});
clearButton.addEventListener("click", clearCalculator);
equalsButton.addEventListener("click", calculate);
backspaceButton.addEventListener("click", backspace);
}
function setupKeyboardInput() {
window.addEventListener(
"keydown",
(event) => {
if (event.defaultPrevented) {
return; // Do nothing if the event was already processed
}
// Check if the pressed key is a number or floating point
const numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."];
if (numbers.includes(event.key)) populateDisplay(event.key);
else {
// Else check for other keys
switch (event.key) {
case "Backspace":
backspace();
break;
case "+":
processInput("+");
break;
case "-":
processInput("-");
break;
case "*":
processInput("*");
break;
case "/":
processInput("/");
break;
case "Enter":
calculate();
break;
default:
return; // Quit when this doesn't handle the key event.
}
}
// Cancel the default action to avoid it being handled twice
event.preventDefault();
},
true
);
// the last option dispatches the event to the listener first,
// then dispatches event to window
}
addButtonEvents();
setupKeyboardInput();
clearCalculator();