main.js 3.7 KB
Newer Older
Joel Florentin committed
1 2 3
const teclas = document.querySelectorAll('.tecla');
var screen = document.getElementById("screen");
var screenAnterior = document.getElementById("screen-anterior");
Joel Florentin committed
4
var entrada_anterior="";
Joel Florentin committed
5
var dot_posible = true;
Joel Florentin committed
6 7

var escribir = function(contenido) {
Joel Florentin committed
8 9 10
    switch (getTipo(contenido)) {
        case "num":
            switch (getTipo(entrada_anterior)) {
Joel Florentin committed
11
                case "num": case "":
Joel Florentin committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
                    screen.value = Number(screen.value + contenido) 
                    break;
                case "op":
                    screen.value = contenido
                    break;
                case "dot":
                    screen.value = Number(screen.value + contenido) 
                    break;
                case "eq":
                    screen.value = contenido
                    screenAnterior.innerHTML = ""
                    break;
            
                default:
                    break;
            }
            
            
            break;
        
        case "op": case "eq":
            let ls = screenAnterior.innerHTML.length-1;
            if(screen.value.length==0 && screenAnterior.innerHTML.length==0) return;
            switch (getTipo(entrada_anterior)) {
                case "num": case "dot":
                    if(esOperador(screenAnterior.innerHTML[ls])){
                        let a = screenAnterior.innerHTML.substring(0,ls);
                        let b = screen.value
                        screen.value = operar(Number(a),Number(b),screenAnterior.innerHTML[ls])
                        if (getTipo(contenido)=="eq") {
                            screenAnterior.innerHTML += b + contenido;
                        } else {
                            screenAnterior.innerHTML = screen.value + contenido;
                        }
                    }
                    else{
                        screenAnterior.innerHTML = Number(screen.value) + contenido;
                    }
                    
                    break;
                case "op":
                    screenAnterior.innerHTML = screenAnterior.innerHTML.substring(0,ls) + contenido;
                    break;

                case "eq":
                    screenAnterior.innerHTML = Number(screen.value) + contenido;
                default:
                    break;
            }
            dot_posible = true;
            
            break;
        
        case "dot":
            if(dot_posible){
                switch (getTipo(entrada_anterior)) {
                    case "num":
                        dot_posible = false;
                        screen.value = screen.value + contenido;
                        break;
                
                    default:
                        break;
                }
            }
           
            break;
    
        default:
            break;
    }
    
    entrada_anterior = contenido;
    
};
function esNumero(valor){
    return !isNaN(Number(valor))
}

function esOperador(valor){
    switch (valor) {
        case "+": case "-": case "/": case "*":
            return true;
        default:
            return false;
    }
}

function getTipo(valor){
    if(esNumero(valor)) return "num";
    if(esOperador(valor)) return "op";
    if(valor=="=") return "eq";
    if(valor==".") return "dot";
Joel Florentin committed
105
    return valor;
Joel Florentin committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
}

function operar(a,b,op){
    switch (op) {
        case "+":
            
            return a + b;
        case "-":
            
            return a - b;
        case "/":
            
            return a / b;
        case "*":
            
            return a * b;
    }
}

Joel Florentin committed
125 126
teclas.forEach(el => el.addEventListener('click', event =>{escribir(event.target.innerHTML)}));
document.addEventListener('keypress',event => {escribir(event.key)})