Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Calculadora = {
- operando1: 'a',
- operando2: 'b',
- operador: '',
- display: '',
- init: function(){
- this.display = document.getElementById('display');
- this.operando1 = 'a';
- this.aplicarEfectoTeclas();
- },
- aplicarEfectoTeclas: function(){
- var teclas = document.getElementsByClassName('tecla');
- for(var i = 0; i <teclas.length; ++i) {
- teclas[i].onclick = this.presionarTecla;
- }
- },
- presionarTecla : function(event){
- var tecla = event.target;
- tecla.style.transform = "scale(0.9)";
- setTimeout(function () {
- tecla.style.transform = "scale(1.0)";
- }, 200);
- switch (tecla.alt){
- case 'On':
- display.textContent = '0';
- break;
- case 'signo':
- if(display.textContent.length > 0){
- if(display.textContent.substr(0, 1) == '-'){
- display.textContent = display.textContent.substr(1);
- } else {
- display.textContent = '-' + display.textContent;
- }
- }
- break;
- case '0':
- if(display.textContent.length > 0 && display.textContent.substr(0, 1) != '0'){
- if(Calculadora.comprobarLongitudDisplay()){
- display.textContent = display.textContent + tecla.alt;
- }
- }
- break;
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- if(Calculadora.comprobarLongitudDisplay()){
- Calculadora.removerCeroDisplay();
- display.textContent = display.textContent + tecla.alt;
- }
- break;
- case 'punto':
- if(display.textContent.length > 0 && display.textContent.indexOf('.') == -1){
- display.textContent = display.textContent + '.';
- }
- break;
- case 'mas':
- Calculadora.operando1 = Number(display.textContent);
- display.textContent = '';
- break;
- case 'igual':
- Calculadora.operando2 = Number(display.textContent);
- var resultado = Calculadora.operando1 + Calculadora.operando2;
- display.textContent = resultado;
- }
- },
- comprobarLongitudDisplay: function(){
- if(this.display.textContent.indexOf('-') != -1){
- return this.display.textContent.length < 9;
- } else {
- return this.display.textContent.length < 8;
- }
- },
- removerCeroDisplay: function () {
- if(this.display.textContent.length == 1 && this.display.textContent == '0') {
- this.display.textContent = '';
- }
- }
- };
- Calculadora.init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement