JS: Clearing, displaying via new function, new key presses

This commit is contained in:
NetMan 2024-01-16 20:22:42 +01:00
parent f74e37364d
commit 588ff13304
1 changed files with 57 additions and 7 deletions

View File

@ -48,7 +48,7 @@ function execute(operation) {
} }
} }
let operation = { const operationInitial = {
a: "0", a: "0",
operator: "", operator: "",
b: "", b: "",
@ -57,15 +57,36 @@ let operation = {
outputOfEquasion: false, outputOfEquasion: false,
} }
let operation = {};
Object.assign(operation, operationInitial);
let readyForEquasion = ""; 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 {
valueGui.textContent = `${operation.b}`;
}
}
function click(value) { function click(value) {
if (!isNaN(+value)) { if (!isNaN(+value)) {
if (operation.outputOfEquasion) { if (operation.outputOfEquasion) {
operation.a = value; operation.a = value;
operation.outputOfEquasion = false; operation.outputOfEquasion = false;
signGui.textContent = ""; populateDisplay(operation);
valueGui.textContent = `${operation.a}`;
operation.addToA = true; operation.addToA = true;
} else { } else {
if (operation.addToA) { if (operation.addToA) {
@ -73,24 +94,24 @@ function click(value) {
operation.a = ""; operation.a = "";
} }
operation.a += value; operation.a += value;
valueGui.textContent = `${operation.a}`; populateDisplay(operation);
} else { } else {
if (operation.b == "0") { if (operation.b == "0") {
operation.b = ""; operation.b = "";
} }
operation.b += value; operation.b += value;
operation.readyForEquasion = true; operation.readyForEquasion = true;
valueGui.textContent = `${operation.b}`; populateDisplay(operation);
} }
} }
} else if (value == "=") { } else if (value == "=") {
execute(operation); execute(operation);
signGui.textContent = value; populateDisplay(operation);
} else { } else {
if (operation.operator == "") { if (operation.operator == "") {
operation.addToA = !operation.addToA; operation.addToA = !operation.addToA;
operation.operator = value; operation.operator = value;
signGui.textContent = value; populateDisplay(operation);
} }
if (operation.addToA) { if (operation.addToA) {
operation.operator = value; operation.operator = value;
@ -165,5 +186,34 @@ function numbers(e) {
let passLetter = e.key.toLowerCase(); let passLetter = e.key.toLowerCase();
if (!isNaN(+passLetter)) { if (!isNaN(+passLetter)) {
click(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);
} }
} }