-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
213 lines (179 loc) · 4.52 KB
/
index.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
const isInt = (char) => {
// determines whether given char/str is a number
if (isNaN(parseFloat(char)) && char != ".") {
return false;
}
else {
return true;
}
}
class UserInput {
constructor() {
this.input = []; // maintains the display
this.len = 0;
this.ops = "+-*/%" // operations
this.res = true; // result flag. Begins diplaying 0, so initially true
this.neg = false; // starting negative sign flag
}
clear() {
// clears input, but doesn't change display
this.input = [];
this.len = 0;
}
updateDisplay() {
// updates display with str
let display = document.getElementById("display");
display.innerHTML = this.input.length != 0 ? this.input.join("") : 0;
display.scrollLeft = display.scrollWidth;
// IF WOULD BE EMPTY, FILL WITH "0" INSTEAD!!!
}
add(char) {
// pushes char onto display
if (this.res) {
this.flipRes();
if (! this.ops.includes(char)) {
this.clear();
}
else {
this.input.push(document.getElementById("display").innerHTML);
this.len++;
}
}
let input = this.input;
let len = this.len;
if (len === 0 && char === "-") {
this.neg = true;
}
else if ((isInt(char) && isInt(input[len-1])) || this.neg ) {
this.input[len-1] = [input[len-1],char].join("");
this.neg = false;
}
else {
this.input.push(char);
this.len++;
}
this.updateDisplay();
}
del() {
let len = this.len
let input = this.input
if (len > 0) {
if (input[len-1].length > 1) {
this.input[len-1] = input[len-1].slice(0,-1);
}
else {
this.input.pop();
this.len--;
}
}
this.updateDisplay();
}
write(str) {
// like add, but for entire strings of characters
for (let i = 0; i < str.length; i++) {
this.add(str[i]);
}
}
parse() {
// parses input up to this point, displaying result
let input = this.input;
let len = this.len;
if (this.res) {
return(Number(document.getElementById("display").innerHTML));
}
switch(len) {
case 0:
return;
case 1:
if (! isInt(input[0]) || input[0].split(".").length > 2) {
throw "invalid input";
}
let res = input[0];
return Number(res);
case 2:
throw "invalid input";
}
let res = input[0];
for (let i = 1; i < len; i += 2) {
res = operate(res, input[i+1], input[i]);
}
document.getElementById("display").innerHTML = Number(res.toFixed(5));
return Number(res.toFixed(5));
}
flipRes() {
// res setter
this.res = ! this.res;
}
getDisplay() {
// input getter
return this.input;
}
}
const operate = (n1, n2, op) => {
// performs given operation on n1 and n2
a = parseFloat(n1);
b = parseFloat(n2);
if (isNaN(a) || isNaN(b) || n1.split(".").length > 2 || n2.split(".").length > 2) {
throw "invalid input";
}
switch(op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
return a / b;
case '%':
return a % b;
default:
throw "invalid input";
}
}
const clearDisplay = (input) => {
// clears display
input.clear();
input.updateDisplay();
}
const parser = (input) => {
try {
res = input.parse();
input.clear();
input.write(res.toString(10));
input.clear();
input.flipRes();
}
catch (err) {
if (err === "invalid input") {
document.getElementById("display").innerHTML = "ERR";
window.setTimeout(function(input) {
input.updateDisplay();
}, 300, input);
}
else {
clearDisplay(input);
throw(err);
}
}
}
const main = () => {
let input = new UserInput();
const buttons = document.getElementById("calculator").children
for (let i = 1; i < buttons.length; i++) {
switch (buttons[i].id) {
case 'eq':
buttons[i].addEventListener("click", () => {parser(input)});
break;
case 'CE':
buttons[i].addEventListener("click", () => {clearDisplay(input)});
break;
case 'C':
buttons[i].addEventListener("click", () => {input.del()});
break;
default:
buttons[i].addEventListener("click", () => {input.add(buttons[i].innerHTML)});
}
}
}
main();