Skip to content

Commit

Permalink
Incluindo o JS
Browse files Browse the repository at this point in the history
Por último e mais importante, incluindo o JavaScript  para ser interativo com o usuário.
  • Loading branch information
Vinicius-Hora-freire committed Apr 7, 2024
1 parent 9e2d145 commit a02c2d6
Showing 1 changed file with 151 additions and 0 deletions.
151 changes: 151 additions & 0 deletions javascript/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
const previousOperationText = document.querySelector("#previous-operacao");
const currentOperationText = document.querySelector("#atual-operacao");
const buttons = document.querySelectorAll("#conteiner-botao button");

class Calculator {
constructor(previousOperationText, currentOperationText) {
this.previousOperationText = previousOperationText;
this.currentOperationText = currentOperationText;
this.currentOperation = "";
}

// adicionar dígito à tela da calculadora
addDigit(digit) {
console.log(digit);

// Verificando se o número já tem um ponto
if (digit === "." && this.currentOperationText.innerText.includes(".")) {
return;
}

this.currentOperation = digit;
this.updateScreen();
}

// processa todas as operações da calculadora
processOperation(operation) {

// Verificando se o valor atual está vazio
if (this.currentOperationText.innerText === "" && operation !== "C") {

// Alterar operação
if (this.previousOperationText.innerText !== "") {
this.changeOperation(operation);
}
return;
}

// Valores atuais e anteriores
let operationValue;
let previous = +this.previousOperationText.innerText.split(" ")[0];
let current = +this.currentOperationText.innerText;

switch (operation) {
case "+":
operationValue = previous + current;
this.updateScreen(operationValue, operation, current, previous);
break;
case "-":
operationValue = previous - current;
this.updateScreen(operationValue, operation, current, previous);
break;
case "*":
operationValue = previous * current;
this.updateScreen(operationValue, operation, current, previous);
break;
case "/":
operationValue = previous / current;
this.updateScreen(operationValue, operation, current, previous);
break;
case "DEL":
this.processDelOperator();
break;
case "CE":
this.processClearCurrentOperator();
break;
case "C":
this.processClearOperator();
break;
case "=":
this.processEqualOperator();
break;
default:
return;
}
}

// Altera os valores da tela da calculadora
updateScreen(
operationValue = null,
operation = null,
current = null,
previous = null
) {
if (operationValue === null) {

// Anexar número ao valor atual
this.currentOperationText.innerText += this.currentOperation;
} else {

// Verificar se o valor é zero, se for apenas adicione o valor atual
if (previous === 0) {
operationValue = current;
}

// Adicionar o valor atual ao anterior
this.previousOperationText.innerText = `${operationValue} ${operation}`;
this.currentOperationText.innerText = "";
}
}

// Alterar operação
changeOperation(operation) {
const mathOperations = ["*", "-", "+", "/"];

if (!mathOperations.includes(operation)) {
return;
}

this.previousOperationText.innerText =
this.previousOperationText.innerText.slice(0, -1) + operation;
}

// Deletar o digito
processDelOperator() {
this.currentOperationText.innerText =
this.currentOperationText.innerText.slice(0, -1);
}

// Limpar operação
processClearCurrentOperator() {
this.currentOperationText.innerText = "";
}

// Limpar todas as operações
processClearOperator() {
this.currentOperationText.innerText = "";
this.previousOperationText.innerText = "";
}

// Processar uma operação
processEqualOperator() {
let operation = this.previousOperationText.innerText.split(" ")[1];

this.processOperation(operation);
}
}

const calc = new Calculator(previousOperationText, currentOperationText);

buttons.forEach((btn) => {
btn.addEventListener("click", (e) => {
const value = e.target.innerText;

if (+value >= 0 || value === ".") {
console.log(value);
calc.addDigit(value);
} else {
calc.processOperation(value);
}
});
});

0 comments on commit a02c2d6

Please sign in to comment.