Advertisement
Taran

Lesson 7 - Calculator

Nov 21st, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Calculator  = function(el){
  2.  
  3.     var input = el.getElementsByTagName('input')[0];
  4.     var buts  = el.getElementsByTagName('button');
  5.     var keybOperations = {43:'+', 47:'/', 42:'*', 45:'-', 99:'C', 61:'='};
  6.     var prevOperation = null;
  7.     var prevNumber = null;
  8.     var operationClicked = false;
  9.     input.value=0;
  10.  
  11.     for(var i = 0 ;  i < buts.length;i++){
  12.         buts[i].onclick=  press
  13.     }
  14.  
  15.     input.onblur = function() {
  16.         input.focus();
  17.     };
  18.  
  19.     input.onkeypress = function(but) {
  20.         but.preventDefault();
  21.         console.log(but.charCode);
  22.         for(key in keybOperations) {
  23.             if (key == but.charCode){
  24.                 operationHandler(keybOperations[key]);
  25.             }
  26.         }
  27.         if( 47 < but.charCode && but.charCode < 58 || but.charCode == 46) {
  28.             var butNum = String.fromCharCode(but.keyCode);
  29.             numberHandler(butNum)
  30.             }
  31.     };
  32.  
  33.     function press(){
  34.         var symbol  = this.innerText;
  35.         if(!isNaN(parseInt(symbol)) || symbol=='.'){
  36.             console.log('number');
  37.             numberHandler(symbol)
  38.         }else{
  39.             console.log('operation');
  40.             operationHandler(symbol)
  41.         }
  42.     }
  43.     function numberHandler(n){
  44.         if(operationClicked){
  45.             input.value="";
  46.             operationClicked = false;
  47.         }
  48.         if(n=='0'&&input.value=='0') return;
  49.         if(n!='0'&&n!='.'&&input.value=='0'){
  50.             input.value=""
  51.         }
  52.  
  53.         input.value=input.value+n;
  54.     }
  55.  
  56.     function operationHandler(o){
  57.         if(o=='C'){
  58.             input.value="0";
  59.             prevOperation = null;
  60.             prevNumber=null;
  61.             return;
  62.         }
  63.         operationClicked = true;
  64.         if(prevNumber)
  65.             switch(prevOperation){
  66.                 case '+':
  67.                     input.value=  prevNumber+ parseFloat(input.value);
  68.                     break;
  69.                 case '/':
  70.                     input.value=  prevNumber/ parseFloat(input.value);
  71.                     break;
  72.                 case '*':
  73.                     input.value=  prevNumber* parseFloat(input.value);
  74.                     break;
  75.                 case '-':
  76.                     input.value=  prevNumber- parseFloat(input.value);
  77.                     break;
  78.             }
  79.         prevNumber =  parseFloat(input.value);
  80.         prevOperation  = o;
  81.     }
  82. };
  83. new Calculator(document.getElementById('container'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement