Skip to content

Commit

Permalink
simplify logarithms, square roots, natural logarithms, and factorial …
Browse files Browse the repository at this point in the history
…functions
  • Loading branch information
rizalfahlevi8 committed Jun 10, 2024
1 parent 20463e1 commit a610e6e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 102 deletions.
8 changes: 4 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,27 @@ <h1 class="text-center mt-3">Scientific Calculator</h1>
</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="factorial()">x!</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>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="percentage()">%</button>
</div>
<div class="row justify-content-center">
<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="math_function('ln')">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>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="operator('/','÷')">÷</button>
</div>
<div class="row justify-content-center">
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="logaritma()">log</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="math_function('log')">log</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="number(4)">4</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="number(5)">5</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="number(6)">6</button>
<button class="btn col-2 mx-md-2 mx-1 mb-2" onclick="operator('*','×')">×</button>
</div>
<div class="row justify-content-center">
<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="math_function('sqrt')"></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 Down
121 changes: 23 additions & 98 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,124 +86,48 @@ function percentage() {
}
}

//------------------ LOGARITMA ------------------------
function logaritma() {
if (data.staging.length !== 0) {
i = data.staging.join('');
log = Math.log10(data.staging.join(''));
data.result = log;
data.formats.push(`log(${i})`);
data.resultformat.push('active');
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;
log = Math.log10(data.result);
data.result = log;
if (data.formats.length > 0 && /(ln|log|fact)/.test(data.formats[data.formats.length - 1])) {
j = data.formats[data.formats.length - 1];
data.formats.pop();
data.formats.push(`log(${j})`);
console.log(j);
} else {
data.formats.push(`log(${i})`);
}
data.resultformat.push('active');
result.value = data.result;
operation.value = data.formats.join('');
console.log(log);
}
}
//----------------- MATH FUNCTION ----------------------
// consisting of logarithms, square roots, natural logs, and factorials
function math_function(operationType) {
const operations = {
log: (x) => Math.log10(x),
ln: (x) => Math.log(x),
fact: (x) => factorialCalculation(x),
sqrt: (x) => Math.sqrt(x)
};

//------------------ LOGNATURAL ------------------------
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('active');
result.value = data.result;
operation.value = data.formats.join('');
data.staging = [];
} else if (data.result !== 0 || data.result === 0) {
i = data.result;
ln = Math.log(data.result);
data.result = ln;
if (data.formats.length > 0 && /(ln|log|fact)/.test(data.formats[data.formats.length - 1])) {
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('active');
result.value = data.result;
operation.value = data.formats.join('');
}
console.log(data);
}
const formatMap = {
log: (i) => `log(${i})`,
ln: (i) => `ln(${i})`,
fact: (i) => `fact(${i})`,
sqrt: (i) => `√(${i})`
};

//------------------ SQUAREROOT ------------------------
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.result = operations[operationType](i);
data.formats.push(formatMap[operationType](i));
data.resultformat.push('active');
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("√")) {
data.result = operations[operationType](i);
if (data.formats.length > 0 && /(ln|log|fact|√)/.test(data.formats[data.formats.length - 1])) {
j = data.formats[data.formats.length - 1];
data.formats.pop();
data.formats.push(`√(${j})`);
data.formats.push(formatMap[operationType](j));
console.log(j);
} else {
data.formats.push(`√(${i})`);
}
data.resultformat.push('active');
result.value = data.result;
operation.value = data.formats.join('');
}
}

//------------------ 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('active');
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 && /(ln|log|fact)/.test(data.formats[data.formats.length - 1])) {
j = data.formats[data.formats.length - 1];
data.formats.pop();
data.formats.push(`fact(${j})`);
} else {
data.formats.push(`fact(${i})`);
data.formats.push(formatMap[operationType](i));
}
data.resultformat.push('active');
result.value = data.result;
operation.value = data.formats.join('');
}
console.log(data);
}

function factorialCalculation(value) {
Expand All @@ -214,6 +138,7 @@ function factorialCalculation(value) {
return result;
}


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

0 comments on commit a610e6e

Please sign in to comment.