Better code organization, user-added points
This commit is contained in:
parent
588ff13304
commit
46a5c4e14c
215
script.js
215
script.js
|
@ -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,62 +44,30 @@ function operate(equals) {
|
|||
}
|
||||
}
|
||||
|
||||
function execute(operation) {
|
||||
if (operation.readyForEquasion) {
|
||||
const result = operate(operation);
|
||||
operation.addToA = !operation.addToA;
|
||||
operation.readyForEquasion = false;
|
||||
operation.operator = "";
|
||||
// check if number is valid
|
||||
|
||||
function checkNumber(number) {
|
||||
return number == " " ? false : !isNaN(+number);
|
||||
}
|
||||
|
||||
// handlers for actions
|
||||
|
||||
function addPoint() {
|
||||
if (operation.addToA) {
|
||||
operation.a = `${result}`;
|
||||
operation.b = "";
|
||||
operation.outputOfEquasion = true;
|
||||
valueGui.textContent = `${operation.a}`;
|
||||
if (!operation.a.includes(".")) {
|
||||
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 {
|
||||
valueGui.textContent = `${operation.b}`;
|
||||
if (!operation.b.includes(".")) {
|
||||
operation.b += ".";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function click(value) {
|
||||
if (!isNaN(+value)) {
|
||||
function handleNumbers(value) {
|
||||
if (operation.outputOfEquasion) {
|
||||
operation.a = value;
|
||||
operation.outputOfEquasion = false;
|
||||
populateDisplay(operation);
|
||||
operation.addToA = true;
|
||||
} else {
|
||||
if (operation.addToA) {
|
||||
|
@ -94,33 +75,116 @@ function click(value) {
|
|||
operation.a = "";
|
||||
}
|
||||
operation.a += value;
|
||||
populateDisplay(operation);
|
||||
operation.displayA = true;
|
||||
} else {
|
||||
if (operation.b == "0") {
|
||||
operation.b = "";
|
||||
}
|
||||
operation.displayA = false;
|
||||
operation.b += value;
|
||||
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 {
|
||||
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 == "") {
|
||||
operation.addToA = !operation.addToA;
|
||||
operation.operator = value;
|
||||
populateDisplay(operation);
|
||||
}
|
||||
if (operation.addToA) {
|
||||
operation.operator = value;
|
||||
}
|
||||
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() {
|
||||
|
||||
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);
|
||||
});
|
||||
|
@ -178,42 +242,3 @@ function 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