Advertisement
Fhernd

app.js

Sep 20th, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Calculadora = {
  2.     operando1: 'a',
  3.     operando2: 'b',
  4.     operador: '',
  5.     display: '',
  6.     init: function(){
  7.         this.display = document.getElementById('display');
  8.         this.operando1 = 'a';
  9.         this.aplicarEfectoTeclas();
  10.     },
  11.     aplicarEfectoTeclas: function(){
  12.         var teclas = document.getElementsByClassName('tecla');
  13.  
  14.         for(var i = 0; i <teclas.length; ++i) {
  15.             teclas[i].onclick = this.presionarTecla;
  16.         }
  17.     },
  18.     presionarTecla : function(event){
  19.         var tecla = event.target;
  20.         tecla.style.transform = "scale(0.9)";
  21.  
  22.         setTimeout(function () {
  23.             tecla.style.transform = "scale(1.0)";
  24.         }, 200);
  25.  
  26.         switch (tecla.alt){
  27.             case 'On':
  28.                 display.textContent = '0';
  29.                 break;
  30.             case 'signo':
  31.                 if(display.textContent.length > 0){
  32.                     if(display.textContent.substr(0, 1) == '-'){
  33.                         display.textContent = display.textContent.substr(1);
  34.                     } else {
  35.                         display.textContent = '-' + display.textContent;
  36.                     }
  37.                 }
  38.                 break;
  39.             case '0':
  40.                 if(display.textContent.length > 0 && display.textContent.substr(0, 1) != '0'){
  41.                     if(Calculadora.comprobarLongitudDisplay()){
  42.                         display.textContent = display.textContent + tecla.alt;
  43.                     }
  44.                 }
  45.                 break;
  46.             case '1':
  47.             case '2':
  48.             case '3':
  49.             case '4':
  50.             case '5':
  51.             case '6':
  52.             case '7':
  53.             case '8':
  54.             case '9':
  55.                 if(Calculadora.comprobarLongitudDisplay()){
  56.                     Calculadora.removerCeroDisplay();
  57.  
  58.                     display.textContent = display.textContent + tecla.alt;
  59.                 }
  60.                 break;
  61.             case 'punto':
  62.                 if(display.textContent.length > 0 && display.textContent.indexOf('.') == -1){
  63.                     display.textContent = display.textContent + '.';
  64.                 }
  65.                 break;
  66.  
  67.             case 'mas':
  68.                 Calculadora.operando1 = Number(display.textContent);
  69.                 display.textContent = '';
  70.                 break;
  71.  
  72.             case 'igual':
  73.                 Calculadora.operando2 = Number(display.textContent);
  74.  
  75.                 var resultado = Calculadora.operando1 + Calculadora.operando2;
  76.  
  77.                 display.textContent = resultado;
  78.  
  79.  
  80.         }
  81.     },
  82.     comprobarLongitudDisplay: function(){
  83.         if(this.display.textContent.indexOf('-') != -1){
  84.             return this.display.textContent.length < 9;
  85.         } else {
  86.             return this.display.textContent.length < 8;
  87.         }
  88.     },
  89.     removerCeroDisplay: function () {
  90.         if(this.display.textContent.length == 1 && this.display.textContent == '0') {
  91.             this.display.textContent = '';
  92.         }
  93.     }
  94. };
  95.  
  96. Calculadora.init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement