Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ body {
height: calc(100vh - 25px);
}

.calc-cotnainer {
.calc-container {
width: 350px;

height: auto;
Expand Down Expand Up @@ -68,6 +68,7 @@ body {
position: relative;
overflow: hidden;
}

.operation .value {
position: absolute;
opacity: 0.9;
Expand Down Expand Up @@ -118,9 +119,11 @@ body {
opacity: 0.8;
font-weight: 500;
}

.calc-switcher div:hover {
color: rgb(255, 17, 0);
}

.calc-switcher div.active-calc {
color: rgb(255, 17, 0);
font-weight: 700;
Expand All @@ -131,7 +134,6 @@ body {

border-radius: 0 0 25px 25px;
height: 100%;
padding-bottom: 10px;
padding: 10px;
}

Expand All @@ -150,7 +152,6 @@ body {
border: none;
outline: none;
border-radius: 10px 10px;
background-color: transparent;

margin: 5px;

Expand All @@ -170,6 +171,7 @@ body {
width: 111px;
border-radius: 50px;
}

.row #delete {
color: #fff;
background-color: #e05046;
Expand Down Expand Up @@ -209,9 +211,11 @@ body {
height: 30px;
transition: all 0.2s ease-in-out;
}

.advance-keys button:hover {
font-weight: 800;
}

/* #D05D66 */
/* disable button */
.advance-keys button:disabled {
Expand All @@ -220,6 +224,7 @@ body {
color: rgba(243, 128, 34, 0.829);
cursor: default;
}

.advance-keys button:disabled:hover {
font-size: 0.9rem;
font-weight: 600;
Expand All @@ -239,18 +244,20 @@ footer p a {
}

@media (max-width: 400px) {
.calc-cotnainer {
.calc-container {
max-width: 300px;
min-width: 250px;

border-radius: 20px;
}

.row button {
width: 40px;
height: 40px;
margin: 5px;
outline: none;
}

.advance-keys button {
font-weight: 600;
width: 40px;
Expand All @@ -260,6 +267,7 @@ footer p a {
.result {
height: 80px;
}

.row #calculate {
width: 100px;
border-radius: 50px;
Expand Down
10 changes: 5 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="css/style.css" />
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" href="css/style.css"/>
<title>d - Calculator</title>
</head>
<body>
<div class="calculator">
<div class="calc-cotnainer">
<div class="calc-container">
<!-- display section -->
<div class="output">
<div class="operation">
Expand Down
80 changes: 38 additions & 42 deletions js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ normalMode.addEventListener("click", () => {
const OPERATIONS = ["+", "-", "*", "/"];
const POWER = "POWER(";
const FACTORIAL = "FACTORIAL(";
const CUBE = "CUBE(";

let data = {
operation: [],
Expand Down Expand Up @@ -317,7 +316,7 @@ function createCalculatorBtns() {
let addedBtns = 0;

calculator_buttons.forEach((button) => {
if (button.name == "ANS") {
if (button.name === "ANS") {
btnPerRow = 5;
addedBtns++;
const lastRow = document.querySelector(".row:last-child");
Expand All @@ -329,7 +328,7 @@ function createCalculatorBtns() {
});
}

if (addedBtns % btnPerRow == 0) {
if (addedBtns % btnPerRow === 0) {
inputElement.innerHTML += `<div class="row"></div>`;
}

Expand All @@ -342,18 +341,15 @@ function createCalculatorBtns() {
addedBtns++;
});
}

createCalculatorBtns();

// disable advance key for normal mode calculator
function disableAdvaceKey() {
const advanceKey = document.querySelectorAll(".advance-keys");
advanceKey.forEach((key) => {
key.childNodes.forEach((btn) => {
if (!SCIENTIFIC_MODE) {
btn.disabled = true;
} else {
btn.disabled = false;
}
btn.disabled = !SCIENTIFIC_MODE;
});
});
data.formula = [];
Expand All @@ -379,43 +375,43 @@ inputElement.addEventListener("click", (e) => {
const targetBtn = e.target;

calculator_buttons.forEach((button) => {
if (button.name == targetBtn.id) calculator(button);
if (button.name === targetBtn.id) calculator(button);
});
});

// the main calculator button
function calculator(button) {
if (button.type == "operator") {
if (button.type === "operator") {
data.operation.push(button.symbol);
data.formula.push(button.formula);
} else if (button.type == "number") {
} else if (button.type === "number") {
data.operation.push(button.symbol);
data.formula.push(button.formula);
} else if (button.type == "trigo_function") {
} else if (button.type === "trigo_function") {
data.operation.push(button.symbol + "(");

data.formula.push(button.formula);
} else if (button.type == "math_function") {
} else if (button.type === "math_function") {
let symbol, formula;
if (button.name == "factorial") {
if (button.name === "factorial") {
symbol = "!";
formula = button.formula;

data.operation.push(symbol);
data.formula.push(formula);
} else if (button.name == "power") {
} else if (button.name === "power") {
symbol = "^(";
formula = button.formula;

data.operation.push(symbol);
data.formula.push(formula);
} else if (button.name == "square") {
} else if (button.name === "square") {
showPowerOnUi(data, button.formula, 2);
} else if (button.name == "cube") {
} else if (button.name === "cube") {
showPowerOnUi(data, button.formula, 3);
} else if (button.name == "quad") {
} else if (button.name === "quad") {
showPowerOnUi(data, button.formula, 4);
} else if (button.name == "x-inverse") {
} else if (button.name === "x-inverse") {
showPowerOnUi(data, button.formula, -1);
} else {
symbol = button.symbol + "(";
Expand All @@ -424,23 +420,23 @@ function calculator(button) {
data.operation.push(symbol);
data.formula.push(formula);
}
} else if (button.type == "key") {
if (button.name == "clear") {
} else if (button.type === "key") {
if (button.name === "clear") {
data.operation = [];
data.formula = [];

updateOutputResult(0);
} else if (button.name == "delete") {
} else if (button.name === "delete") {
data.operation.pop();
data.formula.pop();
} else if (button.name == "rad") {
} else if (button.name === "rad") {
RADIAN = true;
angleToggler();
} else if (button.name == "deg") {
} else if (button.name === "deg") {
RADIAN = false;
angleToggler();
}
} else if (button.type == "calculate") {
} else if (button.type === "calculate") {
let formulaStr = data.formula.join("");

// solve power and factorial calculation
Expand Down Expand Up @@ -497,17 +493,17 @@ function powerBaseGetter(formula, powerIndexes) {
let parenthesisCount = 0;

while (previousIndex >= 0) {
if (formula[previousIndex] == "(") parenthesisCount--;
if (formula[previousIndex] == ")") parenthesisCount++;
if (formula[previousIndex] === "(") parenthesisCount--;
if (formula[previousIndex] === ")") parenthesisCount++;

let isOperator = false;
OPERATIONS.forEach((operator) => {
if (formula[previousIndex] == operator) isOperator = true;
if (formula[previousIndex] === operator) isOperator = true;
});

let isPower = formula[previousIndex] == POWER;
let isPower = formula[previousIndex] === POWER;

if ((isOperator && parenthesisCount == 0) || isPower) break;
if ((isOperator && parenthesisCount === 0) || isPower) break;

base.unshift(formula[previousIndex]);
previousIndex--;
Expand All @@ -527,7 +523,7 @@ function factorialNumGetter(formula, factorialIndexes) {
let nextIndex = factIndex + 1;
let nextInput = formula[nextIndex];

if (nextInput == FACTORIAL) {
if (nextInput === FACTORIAL) {
factSequence++;
return;
}
Expand All @@ -539,15 +535,15 @@ function factorialNumGetter(formula, factorialIndexes) {
let parenthesisCount = 0;

while (previousIndex >= 0) {
if (formula[previousIndex] == "(") parenthesisCount--;
if (formula[previousIndex] == ")") parenthesisCount++;
if (formula[previousIndex] === "(") parenthesisCount--;
if (formula[previousIndex] === ")") parenthesisCount++;

let isOperator = false;
OPERATIONS.forEach((operator) => {
if (formula[previousIndex] == operator) isOperator = true;
if (formula[previousIndex] === operator) isOperator = true;
});

if (isOperator && parenthesisCount == 0) break;
if (isOperator && parenthesisCount === 0) break;

number.unshift(formula[previousIndex]);
previousIndex--;
Expand Down Expand Up @@ -577,7 +573,7 @@ function factorialNumGetter(formula, factorialIndexes) {
function search(formula, keyword) {
let searchResult = [];
formula.forEach((item, index) => {
if (item == keyword) searchResult.push(index);
if (item === keyword) searchResult.push(index);
});

return searchResult;
Expand Down Expand Up @@ -612,11 +608,10 @@ function trigo(callback, angle) {

// inversion trigonometric
function inv_trigo(callback, value) {
if (callback.name != "atan") {
if (value < -1 || value > 1) {
alert("Please enter a number in the randge between -1 to 1 of acos");
return NaN;
}
if (callback.name !== "atan"
&& (value < -1 || value > 1)) {
alert("Please enter a number in the randge between -1 to 1 of acos");
return NaN;
}

let angle = callback(value);
Expand All @@ -629,7 +624,7 @@ function inv_trigo(callback, value) {

// factorial funciton
function factorial(number) {
if (number % 1 != 0) return gamma(number + 1);
if (number % 1 !== 0) return gamma(number + 1);

if (number === 0 || number === 1) return 1;
let result = 1;
Expand All @@ -639,6 +634,7 @@ function factorial(number) {
}
return result;
}

// gamma function
function gamma(n) {
// accurate to about 15 decimal places
Expand Down