Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculadora que funcione con el teclado. aolartes #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Display.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Display {
this.valorAnterior = '';
this.signos = {
sumar: '+',
dividir: '%',
dividir: '/',
multiplicar: 'x',
restar: '-',
}
Expand All @@ -29,7 +29,7 @@ class Display {
computar(tipo) {
this.tipoOperacion !== 'igual' && this.calcular();
this.tipoOperacion = tipo;
this.valorAnterior = this.valorActual || this.valorAnterior;
this.valorAnterior = this.valorActual.toString() || this.valorAnterior;
this.valorActual = '';
this.imprimirValores();
}
Expand Down
4 changes: 4 additions & 0 deletions index.css
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ button:focus {

#valor-anterior {
font-size: 2em;
}

.enter{
display: none;
}
7 changes: 4 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
<div id="valor-actual"></div>
</div>
<button class="col-2" onclick="display.borrarTodo()">C</button>
<button onclick="display.borrar()">&larr;</button>
<button class="operador" value="dividir">%</button>
<button onclick="display.borrar()" class="backspace">&larr;</button>
<button class="operador" value="dividir">/</button>
<button class="numero">7</button>
<button class="numero">8</button>
<button class="numero">9</button>
<button class="operador" value="multiplicar">X</button>
<button class="operador" value="multiplicar">*</button>
<button class="numero">4</button>
<button class="numero">5</button>
<button class="numero">6</button>
Expand All @@ -35,6 +35,7 @@
<button class="col-2 numero">0</button>
<button class="numero">.</button>
<button class="operador" value="igual">=</button>
<button class="operador enter" value="igual">Enter</button>
</div>
</div>
</body>
Expand Down
32 changes: 32 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,38 @@ const displayValorActual = document.getElementById('valor-actual');
const botonesNumeros = document.querySelectorAll('.numero');
const botonesOperadores = document.querySelectorAll('.operador');

document.addEventListener('keydown', function(event) {
const teclaInput = event.key;
if (esTeclaPermitidaNumero(teclaInput)) {
display.agregarNumero(teclaInput);
}else if(esTeclaPermitidaOperador(teclaInput)){
const signos = {
'*': 'multiplicar',
'+': 'sumar',
'-': 'restar',
'/': 'dividir',
'Enter': 'igual'
}
display.computar(signos[teclaInput]);
}else if(teclaInput === 'Backspace'){
display.borrar();
}else if(teclaInput === 'Escape'){
display.borrarTodo();
}
});

function esTeclaPermitidaNumero(tecla) {
const teclasPermitidasNumeros = Array.from(botonesNumeros).map(boton => boton.textContent);
return teclasPermitidasNumeros.includes(tecla);


}

function esTeclaPermitidaOperador(tecla) {
const teclasPermitidasOperadores = Array.from(botonesOperadores).map(boton => boton.textContent);
return teclasPermitidasOperadores.includes(tecla);
}

const display = new Display(displayValorAnterior, displayValorActual);

botonesNumeros.forEach(boton => {
Expand Down