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

213
script.js
View File

@ -18,6 +18,19 @@ function divide(a, b) {
return 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) { function operate(equals) {
switch(equals.operator) { switch(equals.operator) {
case "+": case "+":
@ -31,62 +44,30 @@ function operate(equals) {
} }
} }
function execute(operation) { // check if number is valid
if (operation.readyForEquasion) {
const result = operate(operation); function checkNumber(number) {
operation.addToA = !operation.addToA; return number == " " ? false : !isNaN(+number);
operation.readyForEquasion = false; }
operation.operator = "";
// handlers for actions
function addPoint() {
if (operation.addToA) { if (operation.addToA) {
operation.a = `${result}`; if (!operation.a.includes(".")) {
operation.b = ""; operation.a += ".";
operation.outputOfEquasion = true;
valueGui.textContent = `${operation.a}`;
} }
console.table(operation);
console.log(`Result: ${result}`);
}
}
const operationInitial = {
a: "0",
operator: "",
b: "",
addToA: true,
readyForEquasion: false,
outputOfEquasion: false,
}
let operation = {};
Object.assign(operation, operationInitial);
let readyForEquasion = "";
function clearDisplay() {
signGui.textContent = "";
valueGui.textContent = `0`;
}
function clear() {
clearDisplay();
Object.assign(operation, operationInitial);
}
function populateDisplay(operation) {
signGui.textContent = `${operation.operator}`;
if (operation.addToA) {
valueGui.textContent = `${operation.a}`;
} else { } else {
valueGui.textContent = `${operation.b}`; if (!operation.b.includes(".")) {
operation.b += ".";
}
} }
} }
function click(value) { function handleNumbers(value) {
if (!isNaN(+value)) {
if (operation.outputOfEquasion) { if (operation.outputOfEquasion) {
operation.a = value; operation.a = value;
operation.outputOfEquasion = false; operation.outputOfEquasion = false;
populateDisplay(operation);
operation.addToA = true; operation.addToA = true;
} else { } else {
if (operation.addToA) { if (operation.addToA) {
@ -94,32 +75,115 @@ function click(value) {
operation.a = ""; operation.a = "";
} }
operation.a += value; operation.a += value;
populateDisplay(operation); operation.displayA = true;
} else { } else {
if (operation.b == "0") { if (operation.b == "0") {
operation.b = ""; operation.b = "";
} }
operation.displayA = false;
operation.b += value; operation.b += value;
operation.readyForEquasion = true; operation.readyForEquasion = true;
populateDisplay(operation);
} }
} }
} else if (value == "=") { }
execute(operation);
populateDisplay(operation); 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 { } else {
operation.b = operation.b.substring(0, operation.b.length - 1);
if (operation.b.length < 1) {
operation.b = "0";
}
}
} else {
clear();
}
}
function handleOperators(value) {
if (operation.operator == "") { if (operation.operator == "") {
operation.addToA = !operation.addToA; operation.addToA = !operation.addToA;
operation.operator = value; operation.operator = value;
populateDisplay(operation);
} }
if (operation.addToA) { if (operation.addToA) {
operation.operator = value; operation.operator = value;
} }
operation.outputOfEquasion = false; operation.outputOfEquasion = false;
} }
console.table(operation);
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() {
Object.assign(operation, operationInitial);
signGui.textContent = "";
valueGui.textContent = operation.a;
}
function populateDisplay(operation) {
signGui.textContent = `${operation.operator}`;
if (operation.displayA) {
valueGui.textContent = `${operation.a}`;
} else {
valueGui.textContent = `${operation.b}`;
}
}
// 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}
}
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() { function generateGui() {
@ -168,7 +232,7 @@ function generateGui() {
button.setAttribute("rowspan", buttonProperties.height); button.setAttribute("rowspan", buttonProperties.height);
button.textContent = buttonProperties.display; button.textContent = buttonProperties.display;
button.addEventListener("click", (event) => { button.addEventListener("click", (event) => {
click(event.target.getAttribute("value")); handleAction(event.target.getAttribute("value"));
}); });
tableRow.append(button); tableRow.append(button);
}); });
@ -178,42 +242,3 @@ function generateGui() {
} }
generateGui(); 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);
}
}