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-11 22:20:16 +00:00
|
|
|
let operation = {
|
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-15 17:43:08 +00:00
|
|
|
let readyForEquasion = "";
|
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-14 19:04:41 +00:00
|
|
|
signGui.textContent = "";
|
|
|
|
valueGui.textContent = `${operation.a}`;
|
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;
|
|
|
|
valueGui.textContent = `${operation.a}`;
|
|
|
|
} else {
|
|
|
|
if (operation.b == "0") {
|
|
|
|
operation.b = "";
|
|
|
|
}
|
|
|
|
operation.b += value;
|
2024-01-15 17:43:08 +00:00
|
|
|
operation.readyForEquasion = true;
|
2024-01-14 19:04:41 +00:00
|
|
|
valueGui.textContent = `${operation.b}`;
|
|
|
|
}
|
2024-01-13 19:32:02 +00:00
|
|
|
}
|
|
|
|
} else if (value == "=") {
|
|
|
|
execute(operation);
|
2024-01-14 19:04:41 +00:00
|
|
|
signGui.textContent = value;
|
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-14 19:04:41 +00:00
|
|
|
signGui.textContent = value;
|
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
|
|
|
];
|
|
|
|
|
2024-01-15 17:43:08 +00:00
|
|
|
// const buttonsValues = [
|
|
|
|
// [7,8,9,"+"],
|
|
|
|
// [4,5,6,"-"],
|
|
|
|
// [1,2,3,"*"],
|
|
|
|
// [0,".","=","/"],
|
|
|
|
// ];
|
|
|
|
|
2024-01-13 19:32:02 +00:00
|
|
|
const buttonsDiv = document.querySelector("#buttons");
|
|
|
|
|
2024-01-15 17:43:08 +00:00
|
|
|
// for (let i = 0; i < 4; i++) {
|
|
|
|
// let button_row = document.createElement("div");
|
|
|
|
// button_row.classList.add("buttons-flex");
|
|
|
|
// for (let j = 0; j < 4; j++) {
|
|
|
|
// let button = document.createElement("div");
|
|
|
|
// button.classList.add("button");
|
|
|
|
// button.setAttribute("value", buttonsValues[i][j]);
|
|
|
|
// button.textContent = `${buttonsValues[i][j]}`;
|
|
|
|
// button.addEventListener("click", (event) => {
|
|
|
|
// click(event.target.getAttribute("value"));
|
|
|
|
// });
|
|
|
|
// button_row.append(button);
|
|
|
|
// }
|
|
|
|
// buttonsDiv.append(button_row);
|
|
|
|
// }
|
|
|
|
|
|
|
|
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);
|
|
|
|
// let button_row = document.createElement("div");
|
|
|
|
// button_row.classList.add("buttons-flex");
|
|
|
|
// for (let j = 0; j < 4; j++) {
|
|
|
|
// let button = document.createElement("div");
|
|
|
|
// button.classList.add("button");
|
|
|
|
// button.setAttribute("value", buttonsValues[i][j]);
|
|
|
|
// // button.textContent = `${buttonsValues[i][j]}`;
|
|
|
|
// button.addEventListener("click", (event) => {
|
|
|
|
// click(event.target.getAttribute("value"));
|
|
|
|
// });
|
|
|
|
// button_row.append(button);
|
|
|
|
// }
|
|
|
|
// buttonsDiv.append(button_row);
|
|
|
|
// }
|
|
|
|
// }
|
2024-01-13 19:32:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
generateGui();
|