-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
348 lines (287 loc) · 10.2 KB
/
script.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
window.addEventListener("load", (e) => {
console.log("Loaded...");
localStorage.setItem("initialZero", "true");
localStorage.setItem("current", "0");
calculator.updateDisplay();
});
//Calculator class stores number input, handles display of output;
class Calculator {
constructor(previousOperandTextElement, currentOperandTextElement) {
//sets textElement property values inside the Calculator class;
this.previousOperandTextElement = previousOperandTextElement;
this.currentOperandTextElement = currentOperandTextElement;
this.currentOperand = localStorage.getItem("current");
this.previousOperand = localStorage.getItem("previous");
this.operation = localStorage.getItem("operation");
this.calculated = localStorage.getItem("calculated");
}
clear() {
this.currentOperand = "";
this.previousOperand = "";
this.operation = undefined;
localStorage.clear();
localStorage.setItem("initialZero", "true");
localStorage.setItem("current", "0");
localStorage.setItem("calculated", "false");
localStorage.setItem("cleared", "true");
}
delete() {
let currentLocalStorage = localStorage.getItem("current");
let prevOpText = localStorage.getItem("prevOperandText");
if (localStorage.getItem("initialZero") && localStorage.getItem("computation")) {
localStorage.removeItem("computation");
localStorage.setItem("calculated", "false");
}
if (currentLocalStorage) {
if (currentLocalStorage.length === 1) {
localStorage.setItem("initialZero", "true");
localStorage.setItem("current", "0");
currentLocalStorage = localStorage.getItem("current");
} else {
currentLocalStorage = currentLocalStorage.slice(0, -1);
}
}
if (!currentLocalStorage && prevOpText) {
prevOpText = prevOpText.slice(0, -1);
console.log(prevOpText.length);
if (prevOpText.length == 0) {
localStorage.setItem("initialZero", "true");
localStorage.setItem("current", "0");
currentLocalStorage = localStorage.getItem("current");
console.log(currentLocalStorage)
}
localStorage.setItem("prevOperandText", prevOpText);
}
localStorage.setItem("current", currentLocalStorage);
}
appendNumber(number) {
//add input to current local storage;
let currentLS = localStorage.getItem("current");
currentLS += number.toString();
localStorage.setItem("current", currentLS); // resets/sets new current number;
return;
}
//compute the mathematical expressions and display on screen;
compute() {
let currLocal,
prevOperandText,
toBeComputed,
operator,
currOperand = "",
priorOperand = "";
const regExp = /\+|-|\*|\//;
currLocal = localStorage.getItem("current");
prevOperandText = localStorage.getItem("prevOperandText");
toBeComputed = `${prevOperandText}${currLocal}`;
console.log("tobeComputed:", toBeComputed);
// Equals button pushed...
//3+2 = 6
let memo = {};
let computation = 0;
//use Iterative approach instead of for-loop;
//iterate over each index, computation updated along the way;
let operand = "";
for (let i = 0; i < toBeComputed.length; i++) {
let elem = toBeComputed[i];
console.log("elem:", elem);
//66.2564+.025 + .015
if (regExp.test(elem) || i === toBeComputed.length-1) {
if (i === toBeComputed.length - 1) {
operand += elem;
}
if (memo["priorOperand"] && memo["operator"]) {
memo["currentOperand"] = operand;
}
if (!memo["priorOperand"]) {
memo["priorOperand"] = operand;
}
if (!memo["operator"]) {
memo["operator"] = elem;
}
operand = "";
} else {
//gather element;
operand += elem;
}
if (memo["priorOperand"] && memo["currentOperand"] && memo["operator"]) {
//run calculation;
operator = memo["operator"];
priorOperand = parseFloat(memo["priorOperand"]);
currOperand = parseFloat(memo["currentOperand"]);
console.log(priorOperand, currOperand);
computation = this.runCalculation(priorOperand, operator, currOperand);
memo["priorOperand"] = computation;
memo["currentOperand"] = "";
memo["operator"] = elem;
operator = "";
priorOperand = "";
currOperand = "";
}
}
localStorage.setItem("current", localStorage.getItem("computation"));
localStorage.setItem("prevOperandText", "");
localStorage.setItem("calculated", true);
return;
}
runCalculation(operand1, operation, operand2) {
//perform math computation based on the operation button selected;
let computation = 0;
switch (operation) {
case "+":
computation = operand1 + operand2;
break;
case "-":
computation = operand1 - operand2;
break;
case "*":
computation = operand1 * operand2;
break;
case "/":
computation = operand1 / operand2;
break;
default:
return;
}
localStorage.setItem("computation", computation.toString());
return computation.toString();
}
//updates the display screen;
updateDisplay() {
let regExp = /\./g;
let curr = localStorage.getItem("current");
let opsButton = localStorage.getItem("operationButtonPushed");
if (localStorage.getItem("cleared")) {
this.currentOperandTextElement.innerText =
localStorage.getItem("current");
this.previousOperandTextElement.innerText = "";
}
//update computation display if needed;
if (localStorage.getItem("prevOperandText")) {
this.previousOperandTextElement.innerText =
localStorage.getItem("prevOperandText");
}
//remove initial zero;
if (curr.length > 1) {
if (localStorage.getItem("initialZero") && curr[0] === "0") {
localStorage.setItem("initialZero", "false");
curr = curr.slice(1);
console.log("running...");
}
//remove consecutive initial zeros
if (curr[0] === "0" && curr[1] === "0") {
curr = "0";
}
}
//if more than one decimal, remove previous decimal from curr;
let decimalsArray = curr.match(regExp);
if (decimalsArray) {
if (decimalsArray.length > 1) {
curr = curr.slice(0, -1);
}
}
//Only if operation button was pushed...
if (opsButton === "true") {
//update previousOperandText display
this.previousOperandTextElement.innerText =
localStorage.getItem("prevOperandText");
curr = "";
}
if (curr[0] === ".") {
let zero = "0";
curr = `${zero}${curr}`;
}
localStorage.setItem("current", curr);
this.currentOperandTextElement.innerText = localStorage.getItem("current");
return;
}
}
//calculator app must run based on stored info in local storage;
const numberButtons = document.querySelectorAll("[data-number]");
//css specificity needed for more consistent performance;
const operationButtons = document.querySelectorAll("body > .calculator_grid > button.operation"); //NodeList returned;
const equalsButton = document.querySelector("body > .calculator_grid > button.equals");
const deleteButton = document.querySelector(
"body > .calculator_grid > button.delete"
);
const allClearButton = document.querySelector(
"body > .calculator_grid > button.all-clear");
let previousOperandTextElement = document.querySelector(
"body > .calculator_grid > .output > .previous_operand"
);
let currentOperandTextElement = document.querySelector(
"body > .calculator_grid > .output > .current_operand"
);
let calculator = new Calculator(
previousOperandTextElement,
currentOperandTextElement
);
numberButtons.forEach((button) => {
button.addEventListener("click", () => {
//get rid of initial zero in local storage;
if (localStorage.getItem("initialZero")) {
if (localStorage.getItem("current") === "0") {
localStorage.setItem("current", "");
}
}
if (localStorage.getItem("current") === localStorage.getItem("computation")) {
localStorage.setItem("current", "");
}
// localStorage.setItem("initialZero", "false");
localStorage.setItem("cleared", "false");
localStorage.setItem("operationButtonPushed", "false");
calculator.appendNumber(button.innerText);
calculator.updateDisplay();
});
});
operationButtons.forEach((button) => {
button.addEventListener("click", () => {
let currentLS = localStorage.getItem("current");
let previousOperandTextElementLS = localStorage.getItem("prevOperandText");
let operationButton = button.innerText;
if (currentLS == null || currentLS == "") {
alert("Invalid entry");
return;
}
let lastChar = currentLS.charAt(currentLS.length - 1);
if (lastChar === ".") {
currentLS = currentLS + "0";
}
if (previousOperandTextElementLS == null) previousOperandTextElementLS = "";
//continue math operation:
if (!previousOperandTextElementLS && localStorage.getItem("calculated") && currentLS) {
localStorage.setItem("calculated", "false");
}
previousOperandTextElementLS += currentLS;
previousOperandTextElementLS += operationButton;
currentLS = "";
localStorage.setItem("current", currentLS);
localStorage.setItem("prevOperandText", previousOperandTextElementLS);
localStorage.setItem("operationButtonPushed", "true");
localStorage.setItem("initialZero", "false");
calculator.updateDisplay();
});
});
equalsButton.addEventListener("click", () => {
let currLS = localStorage.getItem("current");
let prevOperandText = localStorage.getItem("prevOperandText");
if (!prevOperandText || !currLS) {
console.log("running")
return
}
let lastChar = currLS.charAt(currLS.length - 1);
if (lastChar === ".") {
currLS = currLS + "0";
localStorage.setItem("current", currLS);
}
localStorage.setItem("operationButtonPushed", "false");
calculator.compute();
calculator.updateDisplay();
});
allClearButton.addEventListener("click", () => {
calculator.clear();
calculator.updateDisplay();
});
deleteButton.addEventListener("click", () => {
calculator.delete();
calculator.updateDisplay();
});