<div id="calc">
<h3>Calculadora</h3>
<div id="display">
<div id="previous-display"></div>
<div id="current-display"></div>
</div>
<div id="buttons-container">
<div class="containerl2">
<button id="percentual">%</button>
<button id="limpCalculo">CE</button>
<button id="limpTudo">C</button>
<button id="apagar" class="BtnSinais">‹</button>
</div>
<div class="containerl3">
<button id="divisaoUm">1/x</button>
<button id="potencia">x²</button>
<button id="raiz">2√x</button>
<button id="sinaldividir" class="BtnSinais">÷</button>
</div>
<div class="containerl4">
<button id="num7" class="botoes-numericos bgwhite">7</button>
<button id="num8" class="botoes-numericos bgwhite">8</button>
<button id="num9" class="botoes-numericos bgwhite">9</button>
<button id="sinalmultiplica" class="BtnSinais">x</button>
</div>
<div class="containerl5">
<button id="num4" class="botoes-numericos bgwhite">4</button>
<button id="num5" class="botoes-numericos bgwhite">5</button>
<button id="num6" class="botoes-numericos bgwhite">6</button>
<button id="sinalsubtrair" class="BtnSinais">-</button>
</div>
<div class="containerl6">
<button id="num1" class="botoes-numericos bgwhite">1</button>
<button id="num2" class="botoes-numericos bgwhite">2</button>
<button id="num3" class="botoes-numericos bgwhite">3</button>
<button id="sinalsomar" class="BtnSinais">+</button>
</div>
<div class="containerl7">
<button id="maismenos" class="bgwhite">±</button>
<button id="num0" class="botoes-numericos bgwhite">0</button>
<button id="virg" class="bgwhite">,</button>
<button id="igual" class="BtnIgual">=</button>
</div>
</div>
</div>
const previousDisplayText = document.querySelector("#previous-display");
const currentDisplayText = document.querySelector('#current-display')
const buttons = document.querySelectorAll('#buttons-container button')
console.log(buttons);
class Calculator{
constructor(previousDisplayText, currentDisplayText){
this.previousDisplayText = previousDisplayText //sempre sai na tela
this.currentDisplayText = currentDisplayText//sempre sai na tela
this.currentOperation = "" // valor que o usuário está digitando
}
//adicionando digito no display
addDigit(digit){
//verificando se já existe "," na operação ou se a mesma está sendo inserida novamente pelo usuário
if(digit === "," && this.currentDisplayText.innerText.includes(",")){
return;
}
this.currentOperation = digit;
this.updateScreen();
}
// Todas as operações da calculadora
processOperation(operation){
//pegar os valores digitados e o operador
let operationValue;
const previous = +this.previousDisplayText.innerText;
const current = +this.currentDisplayText.innerText;
switch (operation) {
case "+":
operationValue = previous + current;
this.updateScreen(operationValue, operation,current, previous);
break;
default:
break;
}
}
// alterando os valores do display
updateScreen(operationValue = null, operation = null, current = null, previous = null){
console.log(operationValue, operation, current, previous)
this.currentDisplayText.innerText += this.currentOperation;
}
}
const calc = new Calculator(previousDisplayText, currentDisplayText);
buttons.forEach((btn) => {
btn.addEventListener("click", (e) =>{
const value = e.target.innerText;
if( +value >= 0 || value ===","){
calc.addDigit(value);
}else{
calc.processOperation(value);
}
});
})