Better code organization, user-added points

This commit is contained in:
NetMan 2024-01-16 22:11:28 +01:00
parent 588ff13304
commit 46a5c4e14c
1 changed files with 131 additions and 106 deletions

237
script.js
View File

@ -18,6 +18,19 @@ function divide(a, b) {
return a / b;
}
const operationInitial = {
a: "0",
operator: "",
b: "",
addToA: true,
displayA: true,
readyForEquasion: false,
outputOfEquasion: false,
}
let operation = {};
Object.assign(operation, operationInitial);
function operate(equals) {
switch(equals.operator) {
case "+":
@ -31,96 +44,147 @@ function operate(equals) {
}
}
function execute(operation) {
if (operation.readyForEquasion) {
const result = operate(operation);
operation.addToA = !operation.addToA;
operation.readyForEquasion = false;
operation.operator = "";
if (operation.addToA) {
operation.a = `${result}`;
operation.b = "";
operation.outputOfEquasion = true;
valueGui.textContent = `${operation.a}`;
// check if number is valid
function checkNumber(number) {
return number == " " ? false : !isNaN(+number);
}
// handlers for actions
function addPoint() {
if (operation.addToA) {
if (!operation.a.includes(".")) {
operation.a += ".";
}
} else {
if (!operation.b.includes(".")) {
operation.b += ".";
}
console.table(operation);
console.log(`Result: ${result}`);
}
}
const operationInitial = {
a: "0",
operator: "",
b: "",
addToA: true,
readyForEquasion: false,
outputOfEquasion: false,
function handleNumbers(value) {
if (operation.outputOfEquasion) {
operation.a = value;
operation.outputOfEquasion = false;
operation.addToA = true;
} else {
if (operation.addToA) {
if (operation.a == "0") {
operation.a = "";
}
operation.a += value;
operation.displayA = true;
} else {
if (operation.b == "0") {
operation.b = "";
}
operation.displayA = false;
operation.b += value;
operation.readyForEquasion = true;
}
}
}
let operation = {};
Object.assign(operation, operationInitial);
function handleBackspace() {
if (!operation.outputOfEquasion) {
if (operation.addToA) {
operation.a = operation.a.substring(0, operation.a.length - 1);
if (operation.a.length < 1) {
operation.a = "0";
}
} else {
operation.b = operation.b.substring(0, operation.b.length - 1);
if (operation.b.length < 1) {
operation.b = "0";
}
}
} else {
clear();
}
}
let readyForEquasion = "";
function handleOperators(value) {
if (operation.operator == "") {
operation.addToA = !operation.addToA;
operation.operator = value;
}
if (operation.addToA) {
operation.operator = value;
}
operation.outputOfEquasion = false;
}
function clearDisplay() {
signGui.textContent = "";
valueGui.textContent = `0`;
function execute(operation) {
if (operation.readyForEquasion) {
const result = operate(operation);
operation.displayA = true;
operation.addToA = true;
operation.readyForEquasion = false;
operation.operator = "";
operation.a = `${result}`;
operation.b = "";
operation.outputOfEquasion = true;
}
}
function clear() {
clearDisplay();
Object.assign(operation, operationInitial);
signGui.textContent = "";
valueGui.textContent = operation.a;
}
function populateDisplay(operation) {
signGui.textContent = `${operation.operator}`;
if (operation.addToA) {
if (operation.displayA) {
valueGui.textContent = `${operation.a}`;
} else {
valueGui.textContent = `${operation.b}`;
}
}
function click(value) {
if (!isNaN(+value)) {
if (operation.outputOfEquasion) {
operation.a = value;
operation.outputOfEquasion = false;
populateDisplay(operation);
operation.addToA = true;
} else {
if (operation.addToA) {
if (operation.a == "0") {
operation.a = "";
}
operation.a += value;
populateDisplay(operation);
} else {
if (operation.b == "0") {
operation.b = "";
}
operation.b += value;
operation.readyForEquasion = true;
populateDisplay(operation);
}
}
} else if (value == "=") {
execute(operation);
populateDisplay(operation);
} else {
if (operation.operator == "") {
operation.addToA = !operation.addToA;
operation.operator = value;
populateDisplay(operation);
}
if (operation.addToA) {
operation.operator = value;
}
operation.outputOfEquasion = false;
// handling presses on keyboard and buttons
function handleAction(e) {
if (!e.keyCode) {
// when the press comes from button, makes it
// compatible with code for keyboard with no rewrites
e = {key: e}
}
console.table(operation);
let action = true;
let passLetter = e.key.toLowerCase();
if (passLetter == " "
|| passLetter == ".") {
addPoint();
} else if (!isNaN(+passLetter)) {
handleNumbers(passLetter);
} else if (passLetter == "backspace"
|| passLetter == "x") {
handleBackspace();
} else if (passLetter == "+"
|| passLetter == "-"
|| passLetter == "*"
|| passLetter == "/") {
handleOperators(passLetter);
} else if (passLetter == "enter"
|| passLetter == "=") {
execute(operation);
} else if (passLetter == "escape"
|| passLetter == "c") {
clear();
} else {
action = false;
}
if (action) {
populateDisplay(operation);
};
}
document.addEventListener("keydown", handleAction);
// create buttons
function generateGui() {
const allButtons = [
@ -168,7 +232,7 @@ function generateGui() {
button.setAttribute("rowspan", buttonProperties.height);
button.textContent = buttonProperties.display;
button.addEventListener("click", (event) => {
click(event.target.getAttribute("value"));
handleAction(event.target.getAttribute("value"));
});
tableRow.append(button);
});
@ -177,43 +241,4 @@ function generateGui() {
buttonsDiv.append(table);
}
generateGui();
document.addEventListener("keydown", numbers);
function numbers(e) {
e = e || window.event;
let passLetter = e.key.toLowerCase();
if (!isNaN(+passLetter)) {
click(passLetter);
} else if (passLetter == "escape") {
clear();
} else if (passLetter == "backspace") {
if (!operation.outputOfEquasion) {
if (operation.addToA) {
operation.a = operation.a.substring(0, operation.a.length - 1);
if (operation.a.length < 1) {
operation.a = "0";
}
} else {
operation.b = operation.b.substring(0, operation.b.length - 1);
if (operation.b.length < 1) {
operation.b = "0";
}
}
populateDisplay(operation);
} else {
clear();
}
} else if (passLetter == "+"
|| passLetter == "-"
|| passLetter == "*"
|| passLetter == "/"
|| passLetter == "=") {
click(passLetter);
} else if (passLetter == "enter") {
click("=");
} else {
console.log(passLetter);
}
}
generateGui();