calculadora.html 10.6 KB
Newer Older
Cesar Giulano Gonzalez Maqueda committed
1 2 3 4 5 6 7 8 9 10 11 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        table, td{
            border: 1px solid black;
        }
        td{
            width: 100px;
            height: 25px;
            text-align: center;
            font-family: "Lucida Console", "Courier New", monospace;
        }
        #screen{
            width: 250px;
            text-align: right;
        }
        #divisor{
            width: 100px;
        }
        body {
            background: #555;
        }

        .content {
            max-width: 500px;
            margin: auto;
            background: white;
            padding: 10px;
        }
    </style>

    <meta charset="UTF-8">
    <title>Calculadora</title>
</head>
<body>
    <div class="content">
        <table>
            <tr>
                <td id="screen"></td>
                <td id="divisor">/</td>
                <td id="cc">CE</td>
            </tr>
        </table>
        <table>
            <tr>
                <td id="siete">7</td>
                <td id="ocho">8</td>
                <td id="nueve">9</td>
                <td id="mas">+</td>
            </tr>
            <tr>
                <td id="cuatro">4</td>
                <td id="cinco">5</td>
                <td id="seis">6</td>
                <td id="menos">-</td>
            </tr>
            <tr>
                <td id="uno">1</td>
                <td id="dos">2</td>
                <td id="tres">3</td>
                <td id="mult">*</td>
            </tr>
            <tr>
                <td id="dcero">00</td>
                <td id="cero">0</td>
                <td id="punto">.</td>
                <td id="result">=</td>
            </tr>
        </table>
    </div>
</body>
<script>
    (function calculadora() {
        //Cargamos todos las teclas de la calculadora y tambien el screen
        const screen = document.querySelector('#screen');
        const divisor = document.querySelector('#divisor');
        const suma = document.querySelector('#mas');
        const resta = document.querySelector('#menos');
        const multiplicacion = document.querySelector('#mult');
        const uno = document.querySelector('#uno');
        const dos = document.querySelector('#dos');
        const tres = document.querySelector('#tres');
        const cuatro = document.querySelector('#cuatro');
        const cinco = document.querySelector('#cinco');
        const seis = document.querySelector('#seis');
        const siete = document.querySelector('#siete');
        const ocho = document.querySelector('#ocho');
        const nueve = document.querySelector('#nueve');
        const cero = document.querySelector('#cero');
        const doble_cero = document.querySelector('#dcero');
        const punto = document.querySelector('#punto');
        const igual = document.querySelector('#result');
        const ce = document.querySelector('#cc');

        //Esta variable nos servira para guardar numero, operador, numero, respectivamente.
        let params = ["", "", ""];

        let current_param = 0;

        //EventListener que escucha por teclas presionadas para ejecutar en la calculadora
        document.addEventListener("keypress", function onPress(event) {
            switch (event.key){
                case "1":
                    params[current_param] += "1";
                    writeScreen(params)
                    break;
                case "2":
                    params[current_param] += "2";
                    writeScreen(params)
                    break;
                case "3":
                    params[current_param] += "3";
                    writeScreen(params)
                    break;
                case "4":
                    params[current_param] += "4";
                    writeScreen(params)
                    break;
                case "5":
                    params[current_param] += "5";
                    writeScreen(params)
                    break;
                case "6":
                    params[current_param] += "6";
                    writeScreen(params)
                    break;
                case "7":
                    params[current_param] += "7";
                    writeScreen(params)
                    break;
                case "8":
                    params[current_param] += "8";
                    writeScreen(params)
                    break;
                case "9":
                    params[current_param] += "9";
                    writeScreen(params)
                    break;
                case "0":
                    if(!(screen.innerHTML.length === 1 && screen.innerHTML.includes("0"))){
                        params[current_param] += "0";
                        writeScreen(params);
                    }
                    break;
                case "+":
                    escribeOperador("+");
                    break;
                case "-":
                    escribeOperador("-");
                    break;
                case "*":
                    escribeOperador("*");
                    break;
                case "/":
                    escribeOperador("/");
                    break;
                case "=":
                    if(current_param===2){
                        darResultado();
                    }
                    break;
                case "Enter":
                    if(current_param===2){
                        darResultado();
                    }
                    break;
                default:
                    console.log(event.key);
            }
        });

        //Limpiar pantalla, y dejar en estado iniciado la calculadora
        ce.addEventListener("click", ()=>{
            params = ["", "", ""];
            current_param = 0;
            writeScreen(params);
        })

        uno.addEventListener("click", ()=>{
            params[current_param] += "1";
            writeScreen(params);
        })
        dos.addEventListener("click", ()=>{
            params[current_param] += "2";
            writeScreen(params);
        })
        tres.addEventListener("click", ()=>{
            params[current_param] += "3";
            writeScreen(params);
        })
        cuatro.addEventListener("click", ()=>{
            params[current_param] += "4";
            writeScreen(params);
        })
        cinco.addEventListener("click", ()=>{
            params[current_param] += "5";
            writeScreen(params);
        })
        seis.addEventListener("click", ()=>{
            params[current_param] += "6";
            writeScreen(params);
        })
        siete.addEventListener("click", ()=>{
            params[current_param] += "7";
            writeScreen(params);
        })
        ocho.addEventListener("click", ()=>{
            params[current_param] += "8";
            writeScreen(params);
        })
        nueve.addEventListener("click", ()=>{
            params[current_param] += "9";
            writeScreen(params);
        })
        doble_cero.addEventListener("click", ()=>{
            if(screen.innerHTML.length !== 0
                && !(screen.innerHTML.length === 1 && screen.innerHTML.includes("0"))){
                params[current_param] += "00";
                writeScreen(params);
            }
        })
        cero.addEventListener("click", ()=>{
            if(!(screen.innerHTML.length === 1 && screen.innerHTML.includes("0"))){
                params[current_param] += "0";
                writeScreen(params);
            }
        })
        punto.addEventListener("click", ()=>{
            if(!screen.innerHTML.includes(".")){
                params[current_param] += ".";
                writeScreen(params);
            }
        })
        suma.addEventListener("click", ()=>{
            escribeOperador("+")
        });
        resta.addEventListener("click", ()=>{
            escribeOperador("-")
        });
        multiplicacion.addEventListener("click", ()=>{
            escribeOperador("*")
        });
        divisor.addEventListener("click", ()=>{
            escribeOperador("/")
        });

        igual.addEventListener("click", ()=>{
            if(current_param===2){
                darResultado();
            }
        })

        //Funcion que administra el funcionamiento de presionar =
        let darResultado = ()=> {
            let param1 = Number(params[0]);
            let param2 = Number(params[2]);
            let resultado;
            switch (params[1]) {
                case "+":
                    resultado = (param1 + param2).toFixed(2);
                    break;
                case "-":
                    resultado = (param1 - param2).toFixed(2);
                    break;
                case "*":
                    resultado = (param1 * param2).toFixed(2);
                    break;
                case "/":
                    resultado = (param1 / param2).toFixed(2);
                    break;
                default:

            }
            params[0] = ""+resultado;
            current_param = 0;
            params[1] = "";
            params[2] = "";
            writeScreen(params);
        }

        //Funcion que administra el funcionamiento de presionar un operador.
        function escribeOperador(operador){
            if(screen.innerHTML.length !== 0){
                if(current_param === 0){
                    current_param = 2;
                    params[1] += operador;
                    writeScreen(params);
                }else if(current_param === 2){
                    let param1 = Number(params[0]);
                    let param2 = Number(params[2]);
                    let resultado;
                        switch (params[1]) {
                            case "+":
                                resultado = (param1 + param2).toPrecision(2);
                                break;
                            case "-":
                                resultado = (param1 - param2).toPrecision(2);
                                break;
                            case "*":
                                resultado = (param1 * param2).toPrecision(2);
                                break;
                            case "/":
                                resultado = (param1 / param2).toPrecision(2);
                                break;
                            default:

                        }
                        params[0] = ""+resultado;
                        current_param = 2;
                        params[1] = operador;
                        params[2] = "";
                        writeScreen(params);
                }
            }
        }

    })()
    function writeScreen(params){
        const screen = document.querySelector('#screen');
        screen.innerHTML = ""+params[0]+params[1]+params[2];
    }
</script>
</html>