Skip to content

Commit

Permalink
trigonometri done
Browse files Browse the repository at this point in the history
  • Loading branch information
rizalfahlevi8 committed Jun 14, 2024
1 parent 37e28d3 commit 39f2cf3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
9 changes: 6 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ <h1 class="text-center mt-3">Scientific Calculator</h1>
<input id="result" readonly class="border border-0 text-end" type="text">
</div>
<div class="input p-3">
<div class="row justify-content-start">
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="toggleRadDeg()" id="toggleButton" value="rad">rad</button>
</div>
<div class="row justify-content-center">
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="constant('e')">e</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="">sin</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="">cos</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="">tan</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="trigonometri('sin')">sin</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="trigonometri('cos')">cos</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="trigonometri('tan')">tan</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="clearData()">AC</button>
</div>
<div class="row justify-content-center">
Expand Down
54 changes: 53 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const result = document.getElementById('result');
const operation = document.getElementById('operation');
const button = document.getElementById('toggleButton')
let data = {
formats: [],
operations: [],
Expand All @@ -21,6 +22,56 @@ function clearData() {
console.log(data);
}

//------------------ RADIAN OR DEGREES ---------------
function toggleRadDeg() {
if (button.innerText === "rad") {
button.innerText = "deg",
button.value = "deg"
} else {
button.innerText = "rad",
button.innerText = "rad"
}
}

//------------------ 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 @@ -40,6 +91,8 @@ function constant(operationType) {
data.resultformat.push('active');
operation.value = data.formats.join('');
result.value = data.result;
operation.value = data.formats.join('');
data.staging = [];
console.log(data);
}

Expand Down Expand Up @@ -157,7 +210,6 @@ function factorialCalculation(value) {
}
}


//----------------- CALCULATE --------------------------
function calculate() {
if (data.operations[data.operations.length - 1] !== ")") {
Expand Down

0 comments on commit 39f2cf3

Please sign in to comment.