Skip to content

Commit

Permalink
constant done
Browse files Browse the repository at this point in the history
  • Loading branch information
rizalfahlevi8 committed Jun 10, 2024
1 parent a610e6e commit 37e28d3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
10 changes: 5 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ <h1 class="text-center mt-3">Scientific Calculator</h1>
</div>
<div class="input p-3">
<div class="row justify-content-center">
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="percentage()">e</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="factorial()">sin</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="parenthesesOpen()">cos</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="parenthesesClose()">tan</button>
<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="clearData()">AC</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="constant('π')">π</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="math_function('fact')">x!</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="parenthesesOpen()">(</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="parenthesesClose()">)</button>
Expand Down
41 changes: 30 additions & 11 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ console.log(data);
//------------------ CLEARDATA ------------------------
function clearData() {
data.staging = [];
data.result = 0; // Ubah menjadi 0 daripada []
data.result = 0;
data.operations = [];
data.formats = [];
data.resultformat = [];
Expand All @@ -28,14 +28,29 @@ function number(value) {
console.log(data);
}

//------------------ CONSTANT ------------------------
function constant(operationType) {
const operations = {
π: () => Math.PI,
e: () => Math.E,
};

data.result = operations[operationType]();
data.formats.push(operationType);
data.resultformat.push('active');
operation.value = data.formats.join('');
result.value = data.result;
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]))) {
// Jika staging tidak kosong dan nilai terakhir dari operations bukan angka
if (data.formats.length === 0 || (data.formats.length > 0 && !data.formats[data.formats.length - 1].includes(")"))) {
data.operations.push(data.staging.join('')); // Tambahkan nilai staging ke operasi
data.operations.push(data.staging.join(''));
data.formats.push(data.staging.join(''));
data.staging = []; // Kosongkan staging setelah ditambahkan ke operasi
data.staging = [];
formula_str = balanceParentheses(data.operations);
data.result = eval(formula_str.join(''));
operation.value = data.formats.join('');
Expand All @@ -47,7 +62,7 @@ function operator(value, format) {
&& data.operations[data.operations.length - 1] !== "("
&& data.operations[data.operations.length - 1] !== ")"
) {
data.operations.pop(); // Hapus operator terakhir jika ada
data.operations.pop();
data.formats.pop();
} else {
if (data.staging.length !== 0 && data.operations[data.operations.length - 1] !== ")") {
Expand Down Expand Up @@ -131,11 +146,15 @@ function math_function(operationType) {
}

function factorialCalculation(value) {
let result = 1;
for (let i = 2; i <= value; i++) {
result *= i;
if (value < 0) {
return "Invalid input";
}
else if (value == 0) {
return 1;
}
else {
return value * factorialCalculation(value - 1);
}
return result;
}


Expand Down Expand Up @@ -235,7 +254,7 @@ function parenthesesOpen() {
data.staging = [];
operation.value = data.formats.join('');
result.value = data.result;
}else if (data.staging.length === 0 && data.operations[data.operations.length - 1] === ")"){
} else if (data.staging.length === 0 && data.operations[data.operations.length - 1] === ")") {
data.operations.push('*', '(');
data.formats.push('×', '(');
data.staging = [];
Expand All @@ -248,7 +267,7 @@ function parenthesesOpen() {
function balanceParentheses(value) {
data_temporary = [...value];
if (!Array.isArray(data_temporary)) {
console.error("Data yang diterima bukan array");
console.error("The data received is not an array");
return;
}

Expand Down

0 comments on commit 37e28d3

Please sign in to comment.