-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
400 lines (373 loc) · 14.3 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/* =======================================================
* App Name: Scientific-Calculator
* App URL: https://github.com/rizalfahlevi8/scientific-calculator
* Developer: Muhammad Rizal Fahlevi
======================================================== */
const result = document.getElementById('result');
const operation = document.getElementById('operation');
const buttonRadDeg = document.getElementById('toggleRadDeg');
const buttonSecond = document.getElementById('toggleSecond');
let data = {
formats: [],
operations: [],
result: 0,
resultformat: [],
staging: []
}
console.log(data);
//------------------ CLEARDATA ------------------------
function clearData() {
data.staging = [];
data.result = 0;
data.operations = [];
data.formats = [];
data.resultformat = [];
operation.value = data.formats.join('');
result.value = data.staging.join('');
console.log(data);
}
//------------------ TOGGLE SECOND -------------------
function toggleSecond() {
const button0 = [
{ id: 'trigonometric-sin', newFunction: 'sec', newText: 'sec' },
{ id: 'trigonometric-cos', newFunction: 'csc', newText: 'csc' },
{ id: 'trigonometric-tan', newFunction: 'cot', newText: 'cot' },
];
const button1 = [
{ id: 'trigonometric-sin', newFunction: 'sin', newText: 'sin' },
{ id: 'trigonometric-cos', newFunction: 'cos', newText: 'cos' },
{ id: 'trigonometric-tan', newFunction: 'tan', newText: 'tan' },
];
if (buttonSecond.value === "0") {
button0.forEach(buttonObj => {
const element = document.getElementById(buttonObj.id);
element.setAttribute('onclick', `trigonometric('${buttonObj.newFunction}')`);
element.innerText = buttonObj.newText;
});
buttonSecond.value = "1";
} else {
button1.forEach(buttonObj => {
const element = document.getElementById(buttonObj.id);
element.setAttribute('onclick', `trigonometric('${buttonObj.newFunction}')`);
element.innerText = buttonObj.newText;
});
buttonSecond.value = "0";
}
}
//------------------ RADIAN OR DEGREES ---------------
function toggleRadDeg() {
if (buttonRadDeg.innerText === "rad") {
buttonRadDeg.innerText = "deg",
buttonRadDeg.value = "deg"
} else {
buttonRadDeg.innerText = "rad",
buttonRadDeg.value = "rad"
}
}
//------------------ NUMBER --------------------------
function number(value) {
if(/(active)/.test(data.resultformat[data.resultformat.length - 1])){
data.formats.pop();
}
data.staging.push(value);
result.value = data.staging.join('');
console.log(data);
}
//------------------ CONSTANT ------------------------
function constant(operationType) {
const operations = {
π: () => Math.PI,
e: () => Math.E,
};
data.result = operations[operationType]();
data.formats.push(operationType);
data.resultformat = [];
data.resultformat.push('active');
operation.value = data.formats.join('');
result.value = data.result;
operation.value = data.formats.join('');
data.staging = [];
console.log(data);
}
//------------------ OPERATOR ------------------------
// consists of addition, subtraction, multiplication, division, and powers of numbers functions
function operator(value, format) {
if (data.staging.length !== 0 && isNaN(parseFloat(data.operations[data.operations.length - 1]))) {
if (data.formats.length === 0 || (data.formats.length > 0 && !data.formats[data.formats.length - 1].includes(")"))) {
data.operations.push(data.staging.join(''));
data.formats.push(data.staging.join(''));
data.staging = [];
formula_str = balanceParentheses(data.operations);
data.result = eval(formula_str.join(''));
operation.value = data.formats.join('');
result.value = data.result;
}
} else {
if (data.operations.length > 0 && data.resultformat.length === 0
&& isNaN(parseFloat(data.operations[data.operations.length - 1]))
&& data.operations[data.operations.length - 1] !== "("
&& data.operations[data.operations.length - 1] !== ")"
) {
data.operations.pop();
data.formats.pop();
} else {
if (data.staging.length !== 0 && data.operations[data.operations.length - 1] !== ")") {
data.operations.push(data.staging.join(''));
data.formats.push(data.staging.join(''));
data.result = parseFloat(data.staging.join(''));
data.staging = [];
} else if (data.operations[data.operations.length - 1] !== ")") {
data.operations = data.operations.concat(data.result);
if (data.resultformat.length === 0) {
data.formats = data.formats.concat(data.result);
result.value = data.result;
} else {
data.resultformat = [];
}
}
}
}
data.resultformat = [];
data.operations.push(value);
data.formats.push(format)
operation.value = data.formats.join('');
console.log(data);
}
//------------------ PERCENTAGE ------------------------
function percentage() {
if (data.staging.length !== 0) {
persent = data.staging.join('') * 0.01;
data.staging = [];
data.result = persent;
result.value = data.result;
} else if (data.result.length !== 0) {
persent = data.result * 0.01;
data.result = persent;
result.value = data.result;
}
}
//----------------- MATH FUNCTION ----------------------
// consisting of logarithms, square roots, natural logs, and factorials
function math_function(operationType) {
const operations = {
log: (x) => Math.log10(x),
ln: (x) => Math.log(x),
fact: (x) => factorialCalculation(x),
sqrt: (x) => Math.sqrt(x)
};
const formatMap = {
log: (i) => `log(${i})`,
ln: (i) => `ln(${i})`,
fact: (i) => `fact(${i})`,
sqrt: (i) => `√(${i})`
};
if (data.staging.length !== 0) {
i = data.staging.join('');
data.result = operations[operationType](i);
data.formats.push(formatMap[operationType](i));
data.resultformat = [];
data.resultformat.push('active');
result.value = data.result;
operation.value = data.formats.join('');
data.staging = [];
console.log(data);
} else if (data.result !== 0 || data.result === 0) {
i = data.result;
data.result = operations[operationType](i);
if (data.formats.length > 0 && /(active)/.test(data.resultformat[data.resultformat.length - 1])) {
j = data.formats[data.formats.length - 1];
data.formats.pop();
data.formats.push(formatMap[operationType](j));
console.log(j);
} else {
data.formats.push(formatMap[operationType](i));
}
data.resultformat = [];
data.resultformat.push('active');
result.value = data.result;
operation.value = data.formats.join('');
}
console.log(data);
}
function factorialCalculation(value) {
if (value < 0) {
return "Invalid input";
}
else if (value == 0) {
return 1;
}
else {
return value * factorialCalculation(value - 1);
}
}
//------------------ TRIGONOMETRIC --------------------
function trigonometric(value) {
const operations = {
sin: (x) => Math.sin(x),
cos: (x) => Math.cos(x),
tan: (x) => Math.tan(x),
sec: (x) => 1 / Math.cos(x),
csc: (x) => 1 / Math.sin(x),
cot: (x) => 1 / Math.tan(x),
};
const formats = {
sin: (i, unit) => unit === 'deg' ? `sin₀(${i})` : `sinᵣ(${i})`,
cos: (i, unit) => unit === 'deg' ? `cos₀(${i})` : `cosᵣ(${i})`,
tan: (i, unit) => unit === 'deg' ? `tan₀(${i})` : `tanᵣ(${i})`,
sec: (i, unit) => unit === 'deg' ? `sec₀(${i})` : `secᵣ(${i})`,
csc: (i, unit) => unit === 'deg' ? `csc₀(${i})` : `cscᵣ(${i})`,
cot: (i, unit) => unit === 'deg' ? `cot₀(${i})` : `cotᵣ(${i})`,
};
if (data.staging.length !== 0) {
let angleInRadians = buttonRadDeg.value === 'deg' ? data.staging.join('') * (Math.PI / 180) : data.staging.join('');
data.result = operations[value](angleInRadians);
data.formats.push(formats[value](data.staging.join(''), buttonRadDeg.value));
data.resultformat = [];
data.resultformat.push('active');
result.value = data.result;
operation.value = data.formats.join('');
data.staging = [];
} else if (data.result !== 0 || data.result === 0) {
let i = data.result;
let angleInRadians = buttonRadDeg.value === 'deg' ? data.result * (Math.PI / 180) : data.result;
data.result = operations[value](angleInRadians);
if (data.formats.length > 0 && /(active)/.test(data.resultformat[data.resultformat.length - 1])) {
let j = data.formats[data.formats.length - 1];
data.formats.pop();
data.formats.push(formats[value](j, buttonRadDeg.value));
} else {
data.formats.push(formats[value](i, buttonRadDeg.value));
}
data.resultformat = [];
data.resultformat.push('active');
result.value = data.result;
operation.value = data.formats.join('');
}
}
//----------------- CALCULATE --------------------------
function calculate() {
if (data.operations[data.operations.length - 1] !== ")") {
if (data.staging.length !== 0) {
data.operations = data.operations.concat(data.staging);
data.formats = data.formats.concat(data.staging);
} else {
data.operations = data.operations.concat(data.result);
if (data.resultformat.length === 0) {
data.formats = data.formats.concat(data.result);
} else {
data.resultformat = [];
}
}
}
data.staging = [];
formula_str = balanceParentheses(data.operations);
try {
data.result = eval(formula_str.join(''));
data.operations.push('=');
data.formats.push('=');
operation.value = data.formats.join('');
result.value = data.result;
data.operations = [];
data.formats = [];
} catch (error) {
if (error instanceof SyntaxError) {
result.value = "Syntax Error!"
return;
}
}
console.log(data)
}
//disable calculate button
const buttons = document.querySelectorAll("button");
buttons.forEach(button => {
button.addEventListener("click", function () {
if (button.id === "calculate") {
button.disabled = true;
} else {
document.getElementById("calculate").disabled = false;
}
});
});
//------------------ PARENTHESES CLOSE ---------------------
function parenthesesClose() {
let jumlah_open = data.operations.filter(function (item) {
return item === "(";
}).length;
let jumlah_close = data.operations.filter(function (item) {
return item === ")";
}).length;
jumlah = jumlah_open - jumlah_close;
if (jumlah > 0) {
if (/(active)/.test(data.resultformat[data.resultformat.length - 1])) {
data.operations.push(data.result, ')');
data.formats.push(')');
let formula_str = balanceParentheses(data.operations);
data.result = eval(formula_str.join(''));
} else if (data.staging.length !== 0) {
data.operations.push(data.staging.join(''), ')');
data.formats.push(data.staging.join(''), ')');
let formula_str = balanceParentheses(data.operations);
data.result = eval(formula_str.join(''));
} else if (data.staging.length === 0) {
data.operations.push(data.result, ')');
data.formats.push(data.result, ')');
let formula_str = balanceParentheses(data.operations);
data.result = eval(formula_str.join(''));
}
data.staging = [];
data.resultformat = [];
operation.value = data.formats.join('');
result.value = data.result;
}
console.log(data);
}
//------------------ PARENTHESES OPEN ----------------------
function parenthesesOpen() {
if (data.staging.length !== 0 && isNaN(parseFloat(data.operations[data.operations.length - 1]))) {
data.operations.push(data.staging.join(''), '*', '(');
data.formats.push(data.staging.join(''), '×', '(');
formula_str = balanceParentheses(data.operations);
data.result = eval(formula_str.join(''));
} else if (data.staging.length === 0 && /(active)/.test(data.resultformat[data.resultformat.length - 1])) {
data.operations.push(data.result, '*', '(');
data.formats.push('×', '(');
} else if (data.staging.length === 0 && data.operations[data.operations.length - 1] === ")") {
data.operations.push('*', '(');
data.formats.push('×', '(');
} else if (data.staging.length === 0 && isNaN(parseFloat(data.operations[data.operations.length - 1]))) {
data.operations.push('(');
data.formats.push('(');
}
data.staging = [];
data.resultformat = [];
operation.value = data.formats.join('');
result.value = data.result;
console.log(data);
}
function balanceParentheses(value) {
data_temporary = [...value];
if (!Array.isArray(data_temporary)) {
console.error("The data received is not an array");
return;
}
if (data_temporary.length !== 0) {
let jumlah_open = data_temporary.filter(function (item) {
return item === "(";
}).length;
let jumlah_close = data_temporary.filter(function (item) {
return item === ")";
}).length;
let jumlah = jumlah_open - jumlah_close;
if (isNaN(parseFloat(data_temporary[data_temporary.length - 1])) && !data_temporary[data_temporary.length - 1].includes(")")) {
data_temporary.push(1);
for (let i = 0; i <= jumlah - 1; i++) {
data_temporary.push(')');
}
} else {
for (let i = 0; i <= jumlah - 1; i++) {
data_temporary.push(')');
}
}
}
return data_temporary;
}