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-13 23:57:31 +00:00
|
|
|
if (operation.equal) {
|
|
|
|
const result = operate(operation);
|
|
|
|
operation.a_b = !operation.a_b;
|
|
|
|
operation.equal = false;
|
|
|
|
operation.operator = "";
|
|
|
|
if (operation.a_b) {
|
|
|
|
operation.a = `${result}`;
|
|
|
|
operation.b = "";
|
2024-01-14 19:04:41 +00:00
|
|
|
operation.equasion_val = true;
|
|
|
|
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-13 19:32:02 +00:00
|
|
|
a: "",
|
|
|
|
operator: "",
|
|
|
|
b: "",
|
|
|
|
a_b: true,
|
2024-01-13 23:57:31 +00:00
|
|
|
equal: false,
|
2024-01-14 19:04:41 +00:00
|
|
|
equasion_val: false,
|
2024-01-13 19:32:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let equal = "";
|
|
|
|
|
|
|
|
function click(value) {
|
|
|
|
if (!isNaN(+value)) {
|
2024-01-14 19:04:41 +00:00
|
|
|
if (operation.equasion_val) {
|
|
|
|
operation.a = value;
|
|
|
|
operation.equasion_val = !operation.equasion_val;
|
|
|
|
signGui.textContent = "";
|
|
|
|
valueGui.textContent = `${operation.a}`;
|
|
|
|
operation.a_b = true;
|
2024-01-13 19:32:02 +00:00
|
|
|
} else {
|
2024-01-14 19:04:41 +00:00
|
|
|
if (operation.a_b) {
|
|
|
|
if (operation.a == "0") {
|
|
|
|
operation.a = "";
|
|
|
|
}
|
|
|
|
operation.a += value;
|
|
|
|
valueGui.textContent = `${operation.a}`;
|
|
|
|
} else {
|
|
|
|
if (operation.b == "0") {
|
|
|
|
operation.b = "";
|
|
|
|
}
|
|
|
|
operation.b += value;
|
|
|
|
operation.equal = true;
|
|
|
|
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 == "") {
|
|
|
|
operation.a_b = !operation.a_b;
|
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
|
|
|
}
|
|
|
|
if (operation.a_b) {
|
|
|
|
operation.operator = value;
|
2024-01-13 19:32:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
console.table(operation);
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateGui() {
|
|
|
|
|
|
|
|
const buttonsValues = [
|
|
|
|
[7,8,9,"+"],
|
|
|
|
[4,5,6,"-"],
|
|
|
|
[1,2,3,"*"],
|
|
|
|
[0,".","=","/"],
|
|
|
|
];
|
|
|
|
|
|
|
|
const buttonsDiv = document.querySelector("#buttons");
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
generateGui();
|