Skip to content

Commit

Permalink
Improved trigonometri and math function
Browse files Browse the repository at this point in the history
  • Loading branch information
rizalfahlevi8 committed Jun 14, 2024
1 parent 39f2cf3 commit d0c2c7e
Showing 1 changed file with 45 additions and 40 deletions.
85 changes: 45 additions & 40 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,45 +33,6 @@ function toggleRadDeg() {
}
}

//------------------ TRIGONOMETRI --------------------
function trigonometri(value) {
const operations = {
sin: (x) => Math.sin(x),
cos: (x) => Math.cos(x),
tan: (x) => 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})`,
};

if (data.staging.length !== 0) {
let angleInRadians = button.value === 'deg' ? data.staging.join('') * (Math.PI / 180) : data.staging.join('');
data.result = operations[value](angleInRadians);
data.formats.push(formats[value](data.staging.join(''), button.value));
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 = button.value === 'deg' ? data.result * (Math.PI / 180) : data.result;
data.result = operations[value](angleInRadians);
if (data.formats.length > 0 && /(sin|cos|tan)/.test(data.formats[data.formats.length - 1])) {
let j = data.formats[data.formats.length - 1];
data.formats.pop();
data.formats.push(formats[value](j, button.value));
} else {
data.formats.push(formats[value](i, button.value));
}
data.resultformat.push('active');
result.value = data.result;
operation.value = data.formats.join('');
}
}

//------------------ NUMBER --------------------------
function number(value) {
data.staging.push(value);
Expand All @@ -88,6 +49,7 @@ function constant(operationType) {

data.result = operations[operationType]();
data.formats.push(operationType);
data.resultformat = [];
data.resultformat.push('active');
operation.value = data.formats.join('');
result.value = data.result;
Expand Down Expand Up @@ -175,6 +137,7 @@ function math_function(operationType) {
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('');
Expand All @@ -183,14 +146,15 @@ function math_function(operationType) {
} else if (data.result !== 0 || data.result === 0) {
i = data.result;
data.result = operations[operationType](i);
if (data.formats.length > 0 && /(ln|log|fact|√)/.test(data.formats[data.formats.length - 1])) {
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('');
Expand All @@ -210,6 +174,47 @@ function factorialCalculation(value) {
}
}

//------------------ TRIGONOMETRI --------------------
function trigonometri(value) {
const operations = {
sin: (x) => Math.sin(x),
cos: (x) => Math.cos(x),
tan: (x) => 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})`,
};

if (data.staging.length !== 0) {
let angleInRadians = button.value === 'deg' ? data.staging.join('') * (Math.PI / 180) : data.staging.join('');
data.result = operations[value](angleInRadians);
data.formats.push(formats[value](data.staging.join(''), button.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 = button.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, button.value));
} else {
data.formats.push(formats[value](i, button.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] !== ")") {
Expand Down

0 comments on commit d0c2c7e

Please sign in to comment.