2024-01-14 19:04:41 +00:00
|
|
|
const viewDiv = document.querySelector("#view");
|
|
|
|
const signGui = viewDiv.querySelector("#sign");
|
|
|
|
const valueGui = viewDiv.querySelector("#value");
|
|
|
|
|
2024-01-11 22:20:16 +00:00
|
|
|
function add(a, b) {
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
|
|
|
|
function substract(a, b) {
|
|
|
|
return a - b;
|
|
|
|
}
|
|
|
|
|
|
|
|
function multiply(a, b) {
|
|
|
|
return a * b;
|
|
|
|
}
|
|
|
|
|
|
|
|
function divide(a, b) {
|
|
|
|
return a / b;
|
|
|
|
}
|
|
|
|
|
|
|
|
function operate(equals) {
|
|
|
|
switch(equals.operator) {
|
|
|
|
case "+":
|
2024-01-13 19:32:02 +00:00
|
|
|
return add(+equals.a, +equals.b);
|
2024-01-11 22:20:16 +00:00
|
|
|
case "-":
|
2024-01-13 19:32:02 +00:00
|
|
|
return substract(+equals.a, +equals.b);
|
2024-01-11 22:20:16 +00:00
|
|
|
case "*":
|
2024-01-13 19:32:02 +00:00
|
|
|
return multiply(+equals.a, +equals.b);
|
2024-01-11 22:20:16 +00:00
|
|
|
case "/":
|
2024-01-13 19:32:02 +00:00
|
|
|
return divide(+equals.a, +equals.b);
|
2024-01-11 22:20:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-13 19:32:02 +00:00
|
|
|
function execute(operation) {
|
2024-01-15 17:43:08 +00:00
|
|
|
if (operation.readyForEquasion) {
|
2024-01-13 23:57:31 +00:00
|
|
|
const result = operate(operation);
|
2024-01-15 17:43:08 +00:00
|
|
|
operation.addToA = !operation.addToA;
|
|
|
|
operation.readyForEquasion = false;
|
2024-01-13 23:57:31 +00:00
|
|
|
operation.operator = "";
|
2024-01-15 17:43:08 +00:00
|
|
|
if (operation.addToA) {
|
2024-01-13 23:57:31 +00:00
|
|
|
operation.a = `${result}`;
|
|
|
|
operation.b = "";
|
2024-01-15 17:43:08 +00:00
|
|
|
operation.outputOfEquasion = true;
|
2024-01-14 19:04:41 +00:00
|
|
|
valueGui.textContent = `${operation.a}`;
|
2024-01-13 23:57:31 +00:00
|
|
|
}
|
|
|
|
console.table(operation);
|
|
|
|
console.log(`Result: ${result}`);
|
2024-01-13 19:32:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-16 19:22:42 +00:00
|
|
|
const operationInitial = {
|
2024-01-15 17:43:08 +00:00
|
|
|
a: "0",
|
2024-01-13 19:32:02 +00:00
|
|
|
operator: "",
|
|
|
|
b: "",
|
2024-01-15 17:43:08 +00:00
|
|
|
addToA: true,
|
|
|
|
readyForEquasion: false,
|
|
|
|
outputOfEquasion: false,
|
2024-01-13 19:32:02 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 19:22:42 +00:00
|
|
|
let operation = {};
|
|
|
|
Object.assign(operation, operationInitial);
|
|
|
|
|
2024-01-15 17:43:08 +00:00
|
|
|
let readyForEquasion = "";
|
2024-01-13 19:32:02 +00:00
|
|
|
|
2024-01-16 19:22:42 +00:00
|
|
|
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}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-13 19:32:02 +00:00
|
|
|
function click(value) {
|
|
|
|
if (!isNaN(+value)) {
|
2024-01-15 17:43:08 +00:00
|
|
|
if (operation.outputOfEquasion) {
|
2024-01-14 19:04:41 +00:00
|
|
|
operation.a = value;
|
2024-01-15 17:43:08 +00:00
|
|
|
operation.outputOfEquasion = false;
|
2024-01-16 19:22:42 +00:00
|
|
|
populateDisplay(operation);
|
2024-01-15 17:43:08 +00:00
|
|
|
operation.addToA = true;
|
2024-01-13 19:32:02 +00:00
|
|
|
} else {
|
2024-01-15 17:43:08 +00:00
|
|
|
if (operation.addToA) {
|
2024-01-14 19:04:41 +00:00
|
|
|
if (operation.a == "0") {
|
|
|
|
operation.a = "";
|
|
|
|
}
|
|
|
|
operation.a += value;
|
2024-01-16 19:22:42 +00:00
|
|
|
populateDisplay(operation);
|
2024-01-14 19:04:41 +00:00
|
|
|
} else {
|
|
|
|
if (operation.b == "0") {
|
|
|
|
operation.b = "";
|
|
|
|
}
|
|
|
|
operation.b += value;
|
2024-01-15 17:43:08 +00:00
|
|
|
operation.readyForEquasion = true;
|
2024-01-16 19:22:42 +00:00
|
|
|
populateDisplay(operation);
|
2024-01-14 19:04:41 +00:00
|
|
|
}
|
2024-01-13 19:32:02 +00:00
|
|
|
}
|
|
|
|
} else if (value == "=") {
|
|
|
|
execute(operation);
|
2024-01-16 19:22:42 +00:00
|
|
|
populateDisplay(operation);
|
2024-01-13 19:32:02 +00:00
|
|
|
} else {
|
|
|
|
if (operation.operator == "") {
|
2024-01-15 17:43:08 +00:00
|
|
|
operation.addToA = !operation.addToA;
|
2024-01-13 23:57:31 +00:00
|
|
|
operation.operator = value;
|
2024-01-16 19:22:42 +00:00
|
|
|
populateDisplay(operation);
|
2024-01-13 23:57:31 +00:00
|
|
|
}
|
2024-01-15 17:43:08 +00:00
|
|
|
if (operation.addToA) {
|
2024-01-13 23:57:31 +00:00
|
|
|
operation.operator = value;
|
2024-01-13 19:32:02 +00:00
|
|
|
}
|
2024-01-15 17:43:08 +00:00
|
|
|
operation.outputOfEquasion = false;
|
2024-01-13 19:32:02 +00:00
|
|
|
}
|
|
|
|
console.table(operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateGui() {
|
|
|
|
|
2024-01-15 17:43:08 +00:00
|
|
|
const allButtons = [
|
|
|
|
[
|
|
|
|
{ id: "C", width: 1, height: 1, display: "C", },
|
|
|
|
{ id: "X", width: 1, height: 1, display: "X", },
|
|
|
|
{ id: "+", width: 1, height: 1, display: "+", },
|
|
|
|
{ id: "-", width: 1, height: 1, display: "-", },
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{ id: "7", width: 1, height: 1, display: "7", },
|
|
|
|
{ id: "8", width: 1, height: 1, display: "8", },
|
|
|
|
{ id: "9", width: 1, height: 1, display: "9", },
|
|
|
|
{ id: "*", width: 1, height: 1, display: "*", },
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{ id: "4", width: 1, height: 1, display: "4", },
|
|
|
|
{ id: "5", width: 1, height: 1, display: "5", },
|
|
|
|
{ id: "6", width: 1, height: 1, display: "6", },
|
|
|
|
{ id: "/", width: 1, height: 1, display: "/", },
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{ id: "1", width: 1, height: 1, display: "1", },
|
|
|
|
{ id: "2", width: 1, height: 1, display: "2", },
|
|
|
|
{ id: "3", width: 1, height: 1, display: "3", },
|
|
|
|
{ id: "=", width: 1, height: 2, display: "=", },
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{ id: "0", width: 2, height: 1, display: "0", },
|
|
|
|
{ id: ".", width: 1, height: 1, display: ".", },
|
|
|
|
],
|
2024-01-13 19:32:02 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
const buttonsDiv = document.querySelector("#buttons");
|
|
|
|
|
2024-01-15 17:43:08 +00:00
|
|
|
const table = document.createElement("table");
|
|
|
|
table.id = "table";
|
|
|
|
|
|
|
|
allButtons.forEach(row => {
|
|
|
|
const tableRow = document.createElement("tr");
|
|
|
|
row.forEach(buttonProperties => {
|
|
|
|
const button = document.createElement("td");
|
|
|
|
button.setAttribute("value", buttonProperties.id);
|
|
|
|
button.setAttribute("colspan", buttonProperties.width);
|
|
|
|
button.setAttribute("rowspan", buttonProperties.height);
|
|
|
|
button.textContent = buttonProperties.display;
|
2024-01-13 19:32:02 +00:00
|
|
|
button.addEventListener("click", (event) => {
|
|
|
|
click(event.target.getAttribute("value"));
|
|
|
|
});
|
2024-01-15 17:43:08 +00:00
|
|
|
tableRow.append(button);
|
|
|
|
});
|
|
|
|
table.append(tableRow);
|
|
|
|
});
|
|
|
|
buttonsDiv.append(table);
|
2024-01-13 19:32:02 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 22:05:55 +00:00
|
|
|
generateGui();
|
|
|
|
|
|
|
|
document.addEventListener("keydown", numbers);
|
|
|
|
|
|
|
|
function numbers(e) {
|
|
|
|
e = e || window.event;
|
|
|
|
let passLetter = e.key.toLowerCase();
|
|
|
|
if (!isNaN(+passLetter)) {
|
|
|
|
click(passLetter);
|
2024-01-16 19:22:42 +00:00
|
|
|
} 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);
|
2024-01-15 22:05:55 +00:00
|
|
|
}
|
|
|
|
}
|