Skip to content

Commit

Permalink
factorial done
Browse files Browse the repository at this point in the history
  • Loading branch information
rizalfahlevi8 committed Jun 8, 2024
1 parent 6365977 commit df95ade
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 34 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <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">x!</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="factorial()">x!</button>
<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">)</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="percentage()">%</button>
Expand Down
110 changes: 77 additions & 33 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,26 @@ let data = {
}
console.log(data);

//------------------ CLEARDATA ------------------------
function clearData() {
data.staging = [];
data.result = 0; // Ubah menjadi 0 daripada []
data.operations = [];
data.formats = [];
data.resultformat = [];
operation.value = data.formats.join('');
result.value = data.staging.join('');
console.log(data);
}


//------------------ NUMBER ------------------------
function number(value) {
data.staging.push(value);
result.value = data.staging.join('');
console.log(data);
}

//------------------ OPERATOR ------------------------
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
Expand Down Expand Up @@ -62,38 +65,7 @@ function operator(value, format) {
console.log(data);
}


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);
} else {
data.resultformat = [];
}
}
data.staging = [];
formula_str = data.operations.join('');
try {
data.result = eval(formula_str);
data.operations.push('=');
data.formats.push('=');
operation.value = data.formats.join('');
result.value = data.result;
data.operations = [];
data.formats = [];
} catch (error) {
if (error instanceof SyntaxError) {
result = "Syntax Error!"
return;
}
}
console.log(data)
}

//------------------ PERCENTAGE ------------------------
function percentage() {
if (data.staging.length !== 0) {
persent = data.staging.join('') * 0.01;
Expand All @@ -107,6 +79,7 @@ function percentage() {
}
}

//------------------ LOGARITMA ------------------------
function logaritma() {
if (data.staging.length !== 0) {
i = data.staging.join('');
Expand Down Expand Up @@ -137,6 +110,7 @@ function logaritma() {
}
}

//------------------ LOGNATURAL ------------------------
function logNatural() {
if (data.staging.length !== 0) {
i = data.staging.join('');
Expand Down Expand Up @@ -166,6 +140,7 @@ function logNatural() {
}
}

//------------------ SQUAREROOT ------------------------
function squareRoot() {
if (data.staging.length !== 0) {
i = data.staging.join('');
Expand Down Expand Up @@ -195,6 +170,75 @@ function squareRoot() {
}
}

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

function factorialCalculation(value){
let result = 1;
for (let i = 2; i <= value; i++) {
result *= i;
}
return result;
}

//----------------- 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);
} else {
data.resultformat = [];
}
}
data.staging = [];
formula_str = data.operations.join('');
try {
data.result = eval(formula_str);
data.operations.push('=');
data.formats.push('=');
operation.value = data.formats.join('');
result.value = data.result;
data.operations = [];
data.formats = [];
} catch (error) {
if (error instanceof SyntaxError) {
result = "Syntax Error!"
return;
}
}
console.log(data)
}

//disable calculate button
const buttons = document.querySelectorAll("button");

Expand Down

0 comments on commit df95ade

Please sign in to comment.