-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
241 lines (217 loc) · 7.21 KB
/
calculator.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
// TODO: PLEASE refactor, this is such a mess. Yes it works, but ouch!
// start with following the guidelines more closely, using simply operation calls
// and running total. Much simpler that way
let operationHistory = [];
let operationString = "";
let currentNumber = null;
let total = 0;
let decimalListener = true;
let hasDecimal = false;
let zeroError = false;
function operate () {
checkCurrentNumber();
// set initial value for total from first entry
if (operationHistory.length === 2) {
total = operationHistory[0];
updateDisplay();
}
else {
//if last entry is a number
if (!isNaN(operationHistory[operationHistory.length-1])) {
// if at least a number, an operator, and another operator have been entered
if (operationHistory.length >= 3) {
// set tempArr from last two items in operationHistory
let tempArr = operationHistory.slice(operationHistory.length-2,operationHistory.length);
let tempOperation = tempArr.toString();
// replace commas from string conversion and combine with total
tempOperation = tempOperation.replace(/\,/g,"");
tempOperation = total.toString() + tempOperation;
// return error and lock down if dividing by zero
if (tempOperation.slice(tempOperation.length-2,tempOperation.length) === "/0") {
total = "nope";
zeroError = true;
disableAllButtons();
document.getElementById("clearButton").classList.remove("disabled");
}
else {
total = eval(tempOperation);
// limit length of total to 12 characters
if (total.toString().length > 12) {
total = Number(total.toString().substr(0,11));
}
}
}
}
else {
if (operationHistory.length >= 3) {
// set tempArr from last two items in operationHistory
let tempArr = operationHistory.slice(operationHistory.length-3,operationHistory.length-1);
let tempOperation = tempArr.toString();
// replace commas from string conversion and combine with total
tempOperation = tempOperation.replace(/\,/g,"");
tempOperation = total.toString() + tempOperation;
total = eval(tempOperation);
// limit length of total to 12 characters
if (total.toString().length > 12) {
total = Number(total.toString().substr(0,11));
}
}
}
updateDisplay();
}
}
function updateDisplay() {
if (currentNumber === null) {
document.getElementById("displayTotal").innerHTML = total;
}
else {
document.getElementById("displayTotal").innerHTML = currentNumber;
}
document.getElementById("displayOps").innerHTML = operationString;
if (decimalListener === true && hasDecimal === true) {
document.getElementById("decimalButton").removeEventListener("click",decimalCheck);
document.getElementById("decimalButton").classList.add("disabled");
decimalListener = false;
}
else if (decimalListener === false && hasDecimal === false) {
document.getElementById("decimalButton").addEventListener("click",decimalCheck);
document.getElementById("decimalButton").classList.remove("disabled");
decimalListener = true;
}
}
function checkCurrentNumber () {
if (currentNumber != null) {
operationHistory.push(currentNumber);
hasDecimal = false;
currentNumber = null;
operationString = fixTimes(operationHistory).toString();
operationString = operationString.replace(/\,/g," ");
if (operationString.length > 36) {
operationString = operationString.substr(0,36);
}
}
}
function addOperation (input) {
if (currentNumber != null) {
checkCurrentNumber();
}
if(operationHistory.length >=1) {
operationHistory.push(input);
operationString = fixTimes(operationHistory).toString();
operationString = operationString.replace(/\,/g," ");
if (operationString.length > 36) {
operationString = operationString.substr(operationString.length-37,operationString.length-1);
}
updateDisplay();
operate();
}
}
function fixTimes (checkArray) {
let tempArr = Array.from(checkArray);
while (tempArr.indexOf("*") >=0) {
tempArr[tempArr.indexOf("*")] = "x";
}
return tempArr;
}
function updateCurrentNumber (numberAsString) {
if (currentNumber === null) {
currentNumber = numberAsString.toString();
}
else {
if (zeroError === false) {
currentNumber = currentNumber.toString() + numberAsString.toString();
}
}
updateDisplay();
}
function clearAll () {
if (zeroError === true) {
enableAllButtons();
zeroError = false;
}
operationHistory = [];
currentNumber = null;
operationString = "";
total = 0;
updateDisplay();
}
function clearHistory () {
operationString = "";
operationHistory = [];
operationHistory.push(total);
updateDisplay();
}
function backspace() {
if (currentNumber != null) {
currentNumber = currentNumber.slice(0,currentNumber.length-1);
updateDisplay();
}
}
function decimalCheck () {
if (currentNumber.search(/\./) === -1) {
updateCurrentNumber(".");
hasDecimal = true;
updateDisplay();
}
}
function enableAllButtons () {
let hasClassButton = Array.from(document.querySelectorAll(".disabled"));
hasClassButton.forEach(function (node) {
node.classList.remove("disabled");
});
}
function disableAllButtons () {
let isDisabled = Array.from(document.querySelectorAll(".button"));
isDisabled.forEach(function (node) {
node.classList.add("disabled");
});
}
document.getElementById("1Button").addEventListener("click",function () {
updateCurrentNumber('1');
});
document.getElementById("2Button").addEventListener("click",function () {
updateCurrentNumber('2');
});
document.getElementById("3Button").addEventListener("click",function () {
updateCurrentNumber('3');
});
document.getElementById("4Button").addEventListener("click",function () {
updateCurrentNumber('4');
});
document.getElementById("5Button").addEventListener("click",function () {
updateCurrentNumber('5');
});
document.getElementById("6Button").addEventListener("click",function () {
updateCurrentNumber('6');
});
document.getElementById("7Button").addEventListener("click",function () {
updateCurrentNumber('7');
});
document.getElementById("8Button").addEventListener("click",function () {
updateCurrentNumber('8');
});
document.getElementById("9Button").addEventListener("click",function () {
updateCurrentNumber('9');
});
document.getElementById("0Button").addEventListener("click",function () {
updateCurrentNumber('0');
});
document.getElementById("plusButton").addEventListener("click",function () {
addOperation('+');
});
document.getElementById("minusButton").addEventListener("click",function () {
addOperation('-');
});
document.getElementById("timesButton").addEventListener("click",function () {
addOperation('*');
});
document.getElementById("divideButton").addEventListener("click",function () {
addOperation('/')
});
document.getElementById("clearButton").addEventListener("click",clearAll);
document.getElementById("delButton").addEventListener("click",backspace);
document.getElementById("equalsButton").addEventListener("click",function () {
operate();
clearHistory();
});
document.getElementById("decimalButton").addEventListener("click",decimalCheck);