Skip to content

Commit

Permalink
Improved operator and calculation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rizalfahlevi8 committed Jun 10, 2024
1 parent f6ae914 commit d1b09de
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,20 @@ function operator(value, format) {
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] !== "(") {
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(); // Hapus operator terakhir jika ada
data.formats.pop();
} else {
if (data.staging.length !== 0) {
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 {
} 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);
Expand Down Expand Up @@ -213,15 +217,17 @@ function factorialCalculation(value) {

//----------------- CALCULATE --------------------------
function calculate() {
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);
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.resultformat = [];
data.operations = data.operations.concat(data.result);
if (data.resultformat.length === 0) {
data.formats = data.formats.concat(data.result);
} else {
data.resultformat = [];
}
}
}
data.staging = [];
Expand All @@ -236,7 +242,7 @@ function calculate() {
data.formats = [];
} catch (error) {
if (error instanceof SyntaxError) {
result = "Syntax Error!"
result.value = "Syntax Error!"
return;
}
}
Expand Down Expand Up @@ -272,6 +278,7 @@ function parenthesesClose() {
data.formats.push(data.staging.join(''), ')');
formula_str = balanceParentheses(data.operations);
data.result = eval(formula_str.join(''));
data.staging = [];
operation.value = data.formats.join('');
result.value = data.result;
} else if (data.staging.length === 0) {
Expand All @@ -280,6 +287,7 @@ function parenthesesClose() {
formula_str = balanceParentheses(data.operations);
console.log(formula_str);
data.result = eval(formula_str.join(''));
data.staging = [];
operation.value = data.formats.join('');
result.value = data.result;
}
Expand Down Expand Up @@ -322,7 +330,6 @@ function balanceParentheses(value) {
return item === ")";
}).length;
jumlah = jumlah_open - jumlah_close;
console.log(jumlah);

if (isNaN(parseFloat(data_temporary[data_temporary.length - 1])) && !data_temporary[data_temporary.length - 1].includes(")")) {
data_temporary.push(1);
Expand Down

0 comments on commit d1b09de

Please sign in to comment.