Skip to content

Commit

Permalink
squareRoot and logNatural done
Browse files Browse the repository at this point in the history
  • Loading branch information
rizalfahlevi8 committed Jun 8, 2024
1 parent 1beb3ed commit 6365977
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
6 changes: 3 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ <h1 class="text-center mt-3">Scientific Calculator</h1>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="clearData()">AC</button>
</div>
<div class="row justify-content-center">
<button class="btn col-2 mx-md-2 mx-1 mb-2">ln</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="logNatural()">ln</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="number(7)">7</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="number(8)">8</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="number(9)">9</button>
Expand All @@ -44,7 +44,7 @@ <h1 class="text-center mt-3">Scientific Calculator</h1>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="operator('*','x')">×</button>
</div>
<div class="row justify-content-center">
<button class="btn col-2 mx-md-2 mx-1 mb-2"></button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="squareRoot()"></button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="number(1)">1</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="number(2)">2</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="number(3)">3</button>
Expand All @@ -54,7 +54,7 @@ <h1 class="text-center mt-3">Scientific Calculator</h1>
<button class="btn col-2 mx-md-2 mx-1 mb-2">x <span style="position: relative; bottom: .7em; right: .3em; font-size:xx-small;">y</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="number(0)">0</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="number('.')">.</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="calculate()">=</button>
<button id="calculate" class="btn col-2 mx-md-2 mx-1 mb-2" onclick="calculate()">=</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="operator('+','+')">+</button>
</div>
</div>
Expand Down
73 changes: 72 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,75 @@ function logaritma() {
operation.value = data.formats.join('');
console.log(log);
}
}
}

function logNatural() {
if (data.staging.length !== 0) {
i = data.staging.join('');
ln = Math.log(data.staging.join(''));
data.result = ln;
data.formats.push(`ln(${i})`);
data.resultformat.push('aktif');
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;
ln = Math.log(data.result);
data.result = ln;
if (data.formats.length > 0 && data.formats[data.formats.length - 1].includes("ln")) {
j = data.formats[data.formats.length - 1];
data.formats.pop();
data.formats.push(`ln(${j})`);
console.log(j);
} else {
data.formats.push(`ln(${i})`);
}
data.resultformat.push('aktif');
result.value = data.result;
operation.value = data.formats.join('');
}
}

function squareRoot() {
if (data.staging.length !== 0) {
i = data.staging.join('');
sqrt = Math.sqrt(data.staging.join(''));
data.result = sqrt;
data.formats.push(`√(${i})`);
data.resultformat.push('aktif');
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;
sqrt = Math.sqrt(data.result);
data.result = sqrt;
if (data.formats.length > 0 && data.formats[data.formats.length - 1].includes("√")) {
j = data.formats[data.formats.length - 1];
data.formats.pop();
data.formats.push(`√(${j})`);
console.log(j);
} else {
data.formats.push(`√(${i})`);
}
data.resultformat.push('aktif');
result.value = data.result;
operation.value = data.formats.join('');
}
}

//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;
}
});
});

0 comments on commit 6365977

Please sign in to comment.