wibowoow

kalkulator.js

Oct 24th, 2020
736
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const calculator = {
  2.     displayNumber:'0',
  3.     operator: null,
  4.     firstNumber: null,
  5.     waitingForSecondNumber: false
  6. };
  7.  
  8. function updateDisplay(){
  9.     document.querySelector('#displayNumber').innerText = calculator.displayNumber;
  10. }
  11.  
  12. function clearCalculator(){
  13.     calculator.displayNumber = '0';
  14.     calculator.operator = null;
  15.     calculator.firstnumber = null;
  16.     calculator.waitingForSecondNumber=false
  17. }
  18.  
  19. function inputDigit(digit) {
  20.     if (calculator.displayNumber === '0') {
  21.         calculator.displayNumber = digit;
  22.     } else {
  23.         calculator.displayNumber += digit;
  24.     }
  25. }
  26.  
  27. function inverseNumber() {
  28.     if (calculator.displayNumber === '0') {
  29.         return;
  30.     }
  31.     calculator.displayNumber = calculator.displayNumber * -1;
  32. }
  33.  
  34. function handleOperator(operator){
  35.     if(!calculator.waitingForSecondNumber) {
  36.         calculator.operator = operator;
  37.         calculator.waitingForSecondNumber = true;
  38.         calculator.firstNumber = calculator.displayNumber;
  39.  
  40.         //mengatur ulang nilai display number supaya tombol selanjutnya dimulai dari angka pertama lagi
  41.         calculator.displayNumber = '0';
  42.     } else {
  43.         alert('Operator sudah ditetapkan')
  44.     }
  45. }
  46.  
  47. function performCalculation() {
  48.     if (calculator.firstNumber == null || calculator.operator == null) {
  49.         alert("Anda belum menetapkan operator");
  50.         return;
  51.     }
  52.  
  53.     let result = 0;
  54.     if (calculator.operator === "+") {
  55.         result = parseInt(calculator.firstNumber) + parseInt(calculator.displayNumber);
  56.     } else {
  57.         result = parseInt(calculator.firstNumber) - parseInt(calculator.displayNumber)
  58.     }
  59.     calculator.displayNumber = result;
  60. }
  61.  
  62. const buttons = document.querySelectorAll(".button");
  63. for (let button of buttons) {
  64.     button.addEventListener('click', function(event) {
  65.  
  66.         //mendapatkan objek elemen yang diklik
  67.         const target = event.target;
  68.  
  69.         if(target.classList.contains('clear')){
  70.             clearCalculator();
  71.             updateDisplay();
  72.             return;
  73.         }
  74.  
  75.         if(target.classList.contains('negative')){
  76.             inverseNumber();
  77.             updateDisplay();
  78.             return;
  79.         }
  80.  
  81.         if(target.classList.contains('equals')){
  82.             performCalculation();
  83.             updateDisplay();
  84.             return;
  85.         }
  86.  
  87.         if(target.classList.contains('operator')){
  88.             hadleOperator(target.innerText);
  89.             return;
  90.         }
  91.  
  92.         inputDigit(target.innerText);
  93.         updateDisplay()
  94.     });
  95. }
  96.  
  97.  
  98.  
  99.  
RAW Paste Data