Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const result = document.querySelector(".result");
  2. const btns = document.querySelectorAll(".btns");
  3.  
  4. let num1 = null;
  5. let oper = null;
  6.  
  7. btns.forEach(function(btns) {
  8.     btns.addEventListener("click", function(){
  9.  
  10.         if(num1 == null) {
  11.             if (btns.id == "btn0" && result.firstChild.nodeValue != ' '){
  12.                 result.firstChild.nodeValue += 0;
  13.             }
  14.  
  15.             for(let i = 1; i<10; i++){
  16.                 if (btns.id == "btn" + i) {
  17.                     result.firstChild.nodeValue += i;
  18.                 }
  19.             }
  20.         }
  21.  
  22.         if(num1 != null) {
  23.             if (btns.id == "btn0" && result.firstChild.nodeValue != ' '){
  24.                 result.firstChild.nodeValue += 0;
  25.             }
  26.  
  27.             for(let i = 1; i<10; i++){
  28.                 if (btns.id == "btn" + i) {
  29.                     result.firstChild.nodeValue += i;
  30.                 }
  31.             }
  32.         }
  33.     });
  34. });
  35.  
  36.  
  37. const calc = document.querySelectorAll(".calc");
  38.  
  39. calc.forEach(function(calc) {
  40.     calc.addEventListener("click", function(){
  41.         if (!(isNaN(result.firstChild.nodeValue))){
  42.             if (calc.id == "btnplus") {
  43.                 num1 = result.firstChild.nodeValue;
  44.                 if (result.firstChild.nodeValue == ' ') {
  45.                     result.firstChild.nodeValue = "+";
  46.                 }
  47.                 else {
  48.                     result.firstChild.nodeValue = " ";
  49.                 }
  50.                 oper = "+";
  51.             }
  52.  
  53.             if (calc.id == "btnminus") {
  54.                 num1 = result.firstChild.nodeValue;
  55.                 if (result.firstChild.nodeValue == ' ') {
  56.                     result.firstChild.nodeValue = "-";
  57.                 }
  58.                 else {result.firstChild.nodeValue = " ";
  59.                 }
  60.                 oper = "-";
  61.             }
  62.  
  63.             if (calc.id == "btnmultiple") {
  64.                 if (result.firstChild.nodeValue != ' '){
  65.                     num1 = result.firstChild.nodeValue;
  66.                     result.firstChild.nodeValue = " ";
  67.                     oper = "*";
  68.                 }
  69.             }
  70.  
  71.             if (calc.id == "btndivide") {
  72.                 if (result.firstChild.nodeValue != ' '){
  73.                     num1 = result.firstChild.nodeValue;
  74.                     result.firstChild.nodeValue = " ";
  75.                     oper = ":";
  76.                 }
  77.             }
  78.         }
  79.     });
  80. });
  81.  
  82. const equals = document.querySelector(".equals");
  83.  
  84. equals.addEventListener("click", function(){
  85.     if (num1 != null && oper != null && result.firstChild.nodeValue != ' ') {
  86.         switch(oper){
  87.    
  88.             case "+":
  89.                 result.firstChild.nodeValue = parseInt(num1) + parseInt(result.firstChild.nodeValue);
  90.                 num1, oper = null;
  91.                 break;
  92.             case "-":
  93.                 result.firstChild.nodeValue = parseInt(num1) - parseInt(result.firstChild.nodeValue);
  94.                 num1, oper = null;
  95.                 break;
  96.             case "*":
  97.                 result.firstChild.nodeValue = parseInt(num1) * parseInt(result.firstChild.nodeValue);
  98.                 num1, oper = null;
  99.                 break;
  100.             case ":":
  101.                 result.firstChild.nodeValue = parseInt(num1) / parseInt(result.firstChild.nodeValue);
  102.                 num1, oper = null;
  103.                 break;
  104.  
  105.         }
  106.     }
  107. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement