-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
206 lines (171 loc) · 4.57 KB
/
scripts.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
const buttons = document.getElementsByClassName('btn');
Array.from(buttons).forEach((button) => {
button.addEventListener('click', () => {
handleClick(button.id);
});
});
const textField = document.querySelector('.interface-text');
function updateScreen(text) {
textField.textContent = text;
}
// The left and right terms, and an index to toggle which is currently edited
let terms = [0, 0];
let i = 0;
let operator = null;
let previous = {
left: null,
right: null,
operator: null,
result: null,
};
function handleClick(input) {
if (input == 'clear') {
clearHandler();
} else if (input == '%') {
percentHandler();
} else if (input == '±') {
negPosHandler();
} else if (input == '=') {
equalsHandler();
} else if (input == '/' || input == '*' || input == '-' || input == '+') {
operatorHandler(input);
} else {
numKeyHandler(input);
}
console.log(terms[0], operator, terms[1]);
updateScreen(terms[i]);
}
function numKeyHandler(input) {
// If there is no operator, input is left, otherwise input is for right
if (!operator) {
i = 0;
} else {
i = 1;
}
// If left and left equal to prev result, set to zero before proceeding
if (i == 0 && terms[i] == previous.result) {
terms[i] = 0;
}
// If current term exceeds max do nothing
if (terms[i].toString().length >= 10) {
return;
}
// If input a point and term contains a point return, otherwise append
if (terms[i].toString().includes('.')) {
if (input == '.') {
return;
}
terms[i] += input;
return;
}
// If current is not 0 just append input
if (Number(terms[i]) !== 0) {
terms[i] += input;
return;
}
// Last we deal with actual 0, which we just overwrite
if (input == '.') {
terms[i] += input;
return;
}
terms[i] = input;
}
function operatorHandler(input) {
let [left, right] = terms;
// if no operator set operator unless left zero
if (!operator) {
if (Number(left) !== 0) {
operator = input;
}
return;
}
// if is operator, but right still zero just overwrite operator
if (right === 0) {
operator = input;
return;
}
// if right term exists, invoke equals behaviour, and replace operator
// input ready for new right term input
equalsHandler();
operator = input;
}
function equalsHandler() {
let [left, right] = terms;
// If there is no curr or prev operator then return else use prev
if (!operator) {
if (!previous.operator) {
return;
}
operator = previous.operator;
}
// Guard agains dividing by zero
if (operator == '/' && right == 0) {
alert("Don't do that!");
clearHandler();
return;
}
// If the left term is zero then return
if (left == 0) {
return;
}
// If no right term, use prev. right or use left on both sides
if (!right) {
if (!previous.right) {
right = left;
} else {
right = previous.right;
}
}
const result = calculate(parseFloat(left), parseFloat(right), operator);
// Update the previous with the values from the just completed operation
previous.left = left;
previous.right = right;
previous.operator = operator;
previous.result = result;
// Set left to result limited by precision and remove trailing 0s
terms[0] = result.toPrecision(8).replace(/(?:\.0+|(\.\d+?)0+)$/, '$1');
// Null out right and operator
terms[1] = 0;
operator = null;
// Ensure the current term is reset to left
i = 0;
}
function percentHandler() {
if (terms[i] == 0) {
return;
}
terms[i] = terms[i] / 100;
}
function negPosHandler() {
if (terms[i] == 0) {
return;
} else if (terms[i] < 0) {
terms[i] = Math.abs(terms[i]);
} else {
terms[i] = -Math.abs(terms[i]);
}
}
function clearHandler() {
terms = [0, 0];
operator = null;
previous.left = null;
previous.right = null;
previous.operator = null;
}
function calculate(a, b, operator) {
let result;
switch (operator) {
case '+':
result = Number(a) + Number(b);
break;
case '-':
result = Number(a) - Number(b);
break;
case '*':
result = Number(a) * Number(b);
break;
default:
result = Number(a) / Number(b);
}
return result;
}