-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
245 lines (222 loc) · 4.79 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
const input_element = document.querySelector(".input");
const output_operation_element = document.querySelector(".operation .value");
const output_result_element = document.querySelector(".result .value");
let calculator_buttons = [
{
name: "delete",
symbol: "Del",
formula: false,
type: "key",
},
{
name: "clear",
symbol: "C",
formula: false,
type: "key",
},
{
name: "percent",
symbol: "%",
formula: "/100",
type: "number",
},
{
name: "division",
symbol: "÷",
formula: "/",
type: "operator",
},
{
name: "7",
symbol: 7,
formula: 7,
type: "number",
},
{
name: "8",
symbol: 8,
formula: 8,
type: "number",
},
{
name: "9",
symbol: 9,
formula: 9,
type: "number",
},
{
name: "multiplication",
symbol: "×",
formula: "*",
type: "operator",
},
{
name: "4",
symbol: 4,
formula: 4,
type: "number",
},
{
name: "5",
symbol: 5,
formula: 5,
type: "number",
},
{
name: "6",
symbol: 6,
formula: 6,
type: "number",
},
{
name: "addition",
symbol: "+",
formula: "+",
type: "operator",
},
,
{
name: "1",
symbol: 1,
formula: 1,
type: "number",
},
{
name: "2",
symbol: 2,
formula: 2,
type: "number",
},
{
name: "3",
symbol: 3,
formula: 3,
type: "number",
},
{
name: "subtraction",
symbol: "–",
formula: "-",
type: "operator",
},
{
name: "0",
symbol: 0,
formula: 0,
type: "number",
},
{
name: "comma",
symbol: ".",
formula: ".",
type: "number",
},
{
name: "calculate",
symbol: "=",
formula: "=",
type: "calculate",
},
];
let data = {
operation: [],
result: [],
};
function createCalculatorButtons() {
const btns_per_row = 4;
let added_btns = 0;
calculator_buttons.forEach((button, index) => {
if (added_btns % btns_per_row == 0) {
input_element.innerHTML += `<div class="row"></div>`;
}
const row = document.querySelector(".row:last-child");
row.innerHTML += `<button id="${button.name}">
${button.symbol}
</button>`;
added_btns++;
});
}
createCalculatorButtons();
input_element.addEventListener("click", (event) => {
const target_btn = event.target;
calculator_buttons.forEach((button) => {
if (button.name == target_btn.id) calculator(button);
});
});
function calculator(button) {
if (button.type == "operator") {
data.operation.push(button.symbol);
data.result.push(button.formula);
} else if (button.type == "number") {
data.operation.push(button.symbol);
data.result.push(button.formula);
} else if (button.type == "key") {
if (button.name == "clear") {
data.operation = [];
data.result = [];
updateOutputResult(0);
} else if (button.name == "delete") {
data.result.pop();
data.operation.pop();
}
} else if (button.type == "calculate") {
// PUSH WHAT'S LEFT IN TEMP TO RESULT AND JOIN RESULT
let result_joined = data.result.join("");
// CLEAR ALL ARRAYS, NO NEED TO SAVE ANYTHING ANYMORE
data.operation = [];
data.result = [];
// CHECK IF THERE WAS A SYNATX ERROR IN THE operation
let result_final;
try {
result_final = eval(result_joined);
} catch (error) {
if (error instanceof SyntaxError) {
result_final = "Syntax Error!";
updateOutputResult(result_final);
return;
}
}
// FORMAT THE RESULT
result_final = formatResult(result_final);
// SAVE RESULT FOR ANY FUTURE USE
data.operation.push(result_final);
data.result.push(result_final);
// UPDATE OUTPUT
updateOutputResult(result_final);
return;
}
updateOutputOperation(data.operation.join(""));
}
function updateOutputOperation(operation) {
output_operation_element.innerHTML = operation;
}
function updateOutputResult(result) {
output_result_element.innerHTML = result;
}
function digitCounter(number) {
return number.toString().length;
}
function isFloat(number) {
return number % 1 != 0;
}
const max_output_number_length = 10;
const output_precision = 5;
function formatResult(result) {
if (digitCounter(result) > max_output_number_length) {
if (isFloat(result)) {
const result_int = parseInt(result);
const result_int_length = digitCounter(result_int);
if (result_int_length > max_output_number_length) {
return result.toPrecision(output_precision);
} else {
const num_digits_after_point =
max_output_number_length - result_int_length;
return result.toFixed(num_digits_after_point);
}
} else {
return result.toPrecision(output_precision);
}
} else {
return result;
}
}