Commit 9c5a2d1d by Joel Florentin

calculadora keydown

parent 2c607208
......@@ -3,14 +3,14 @@
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Page Title</title>
<title>Calculadora</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='main.css'>
</head>
<body>
<p id="screen-anterior"></p>
<input type="text" id="screen">
<input type="text" id="screen" disabled>
<table>
<tr>
<td class="tecla">7</td> <td class="tecla">8</td> <td class="tecla">9</td> <td class="tecla">+</td>
......
#screen-anterior,#screen{
width: 11em;
height: 2em;
margin-left: 30%;
text-align: right;
}
.tecla{
border: 2px solid;
padding: 3px;
height: 2em;
width: 2em;
padding:0px;
text-align: center;
}
table{
margin-left: 30%;
}
\ No newline at end of file
const teclas = document.querySelectorAll('.tecla');
var screen = document.getElementById("screen");
var screenAnterior = document.getElementById("screen-anterior");
var entrada_anterior;
var entrada_anterior="";
var dot_posible = true;
var escribir = function(event) {
let contenido = event.target.innerHTML;
var escribir = function(contenido) {
switch (getTipo(contenido)) {
case "num":
switch (getTipo(entrada_anterior)) {
case "num":
case "num": case "":
screen.value = Number(screen.value + contenido)
break;
case "op":
......@@ -102,6 +102,7 @@ function getTipo(valor){
if(esOperador(valor)) return "op";
if(valor=="=") return "eq";
if(valor==".") return "dot";
return valor;
}
function operar(a,b,op){
......@@ -121,4 +122,5 @@ function operar(a,b,op){
}
}
teclas.forEach(el => el.addEventListener('click', escribir));
\ No newline at end of file
teclas.forEach(el => el.addEventListener('click', event =>{escribir(event.target.innerHTML)}));
document.addEventListener('keypress',event => {escribir(event.key)})
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment