Better code organization, user-added points
This commit is contained in:
parent
588ff13304
commit
46a5c4e14c
237
script.js
237
script.js
|
@ -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,96 +44,147 @@ 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 = "";
|
|
||||||
if (operation.addToA) {
|
// handlers for actions
|
||||||
operation.a = `${result}`;
|
|
||||||
operation.b = "";
|
function addPoint() {
|
||||||
operation.outputOfEquasion = true;
|
if (operation.addToA) {
|
||||||
valueGui.textContent = `${operation.a}`;
|
if (!operation.a.includes(".")) {
|
||||||
|
operation.a += ".";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!operation.b.includes(".")) {
|
||||||
|
operation.b += ".";
|
||||||
}
|
}
|
||||||
console.table(operation);
|
|
||||||
console.log(`Result: ${result}`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const operationInitial = {
|
function handleNumbers(value) {
|
||||||
a: "0",
|
if (operation.outputOfEquasion) {
|
||||||
operator: "",
|
operation.a = value;
|
||||||
b: "",
|
operation.outputOfEquasion = false;
|
||||||
addToA: true,
|
operation.addToA = true;
|
||||||
readyForEquasion: false,
|
} else {
|
||||||
outputOfEquasion: false,
|
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 = {};
|
function handleBackspace() {
|
||||||
Object.assign(operation, operationInitial);
|
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() {
|
function execute(operation) {
|
||||||
signGui.textContent = "";
|
if (operation.readyForEquasion) {
|
||||||
valueGui.textContent = `0`;
|
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() {
|
function clear() {
|
||||||
clearDisplay();
|
|
||||||
Object.assign(operation, operationInitial);
|
Object.assign(operation, operationInitial);
|
||||||
|
signGui.textContent = "";
|
||||||
|
valueGui.textContent = operation.a;
|
||||||
}
|
}
|
||||||
|
|
||||||
function populateDisplay(operation) {
|
function populateDisplay(operation) {
|
||||||
signGui.textContent = `${operation.operator}`;
|
signGui.textContent = `${operation.operator}`;
|
||||||
if (operation.addToA) {
|
if (operation.displayA) {
|
||||||
valueGui.textContent = `${operation.a}`;
|
valueGui.textContent = `${operation.a}`;
|
||||||
} else {
|
} else {
|
||||||
valueGui.textContent = `${operation.b}`;
|
valueGui.textContent = `${operation.b}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function click(value) {
|
// handling presses on keyboard and buttons
|
||||||
if (!isNaN(+value)) {
|
|
||||||
if (operation.outputOfEquasion) {
|
function handleAction(e) {
|
||||||
operation.a = value;
|
if (!e.keyCode) {
|
||||||
operation.outputOfEquasion = false;
|
// when the press comes from button, makes it
|
||||||
populateDisplay(operation);
|
// compatible with code for keyboard with no rewrites
|
||||||
operation.addToA = true;
|
e = {key: e}
|
||||||
} 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;
|
|
||||||
}
|
}
|
||||||
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() {
|
function generateGui() {
|
||||||
|
|
||||||
const allButtons = [
|
const allButtons = [
|
||||||
|
@ -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);
|
||||||
});
|
});
|
||||||
|
@ -177,43 +241,4 @@ function generateGui() {
|
||||||
buttonsDiv.append(table);
|
buttonsDiv.append(table);
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue