Search code examples
javascripttypeerror

"Uncaught TypeError: Cannot read properties of undefined (reading 'toString') when clicking number button


class Calculator {
  constructor(previousOperandTextElement, currentOperandTextElement) {
    this.previousOperandTextElement = previousOperandTextElement
    this.currentOperandTextElement = currentOperandTextElement
    this.clear()
  }

  clear() {
    this.previousOperandTextElement = ''
    this.currentOperandTextElement = ''
    this.operation = undefined
  }

  delete() {

  }

  appendNumber(number) {
    this.currentOperand = this.currentOperand.toString() + number.toString()
  }

  chooseOperator(operation) {

  }

  compute() {

  }

  updateDisplay() {
    this.currentOperandTextElement.innerHTML = this.currentOperand
  }


}

const numberButtons = document.querySelectorAll('[data-number]')
const operationButtons = document.querySelectorAll('[data-operation]')
const equalsButton = document.querySelector('[data-equals]')
const deleteButton = document.querySelector('[data-delete]')
const allClearButton = document.querySelector('[data-all-clear]')
const previousOperandTextElement = document.querySelector('[data-previous-operand]')
const currentOperandTextElement = document.querySelector('[data-current-operand]')

const calculator = new Calculator(previousOperandTextElement, currentOperandTextElement)

numberButtons.forEach(button => {
  button.addEventListener('click', () => {
    calculator.appendNumber(button.innerHTML)
    calculator.updateDisplay()
  })
})
*,
*::before,
*::after {
  box-sizing: border-box;
  font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
  font-weight: normal;
}

body {
  padding: 0;
  margin: 0;
  background: linear-gradient(to right, #00AAFF, #00FF6C);
}

.calculator-grid {
  display: grid;
  justify-content: center;
  align-content: center;
  min-height: 100vh;
  grid-template-columns: repeat(4, 100px);
  grid-template-rows: minmax(120px, auto) repeat(5, 100px);
}

.calculator-grid button {
  cursor: pointer;
  font-size: 2rem;
  border: 1px solid white;
  outline: none;
  background-color: rgba(255, 255, 255, .75);
}

.calculator-grid button:hover {
  background-color: rgba(255, 255, 255, .9);
}

.span-two {
  grid-column: span 2;
}

.output {
  grid-column: 1 / -1;
  background-color: rgba(0, 0, 0, 0.75);
  border: solid 5px green;
  display: flex;
  align-items: flex-end;
  flex-direction: column;
  justify-content: space-between;
  padding: 15px;
}

.output .previous-operand {
  color: rgba(255, 255, 255, .75);
  font-size: 1.5rem;
}

.output .current-operand {
  color: rgba(255, 255, 255, .75);
  font-size: 2.5rem;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Calculator</title>
  <link rel="stylesheet" href="calculator.css">
  <script src="calculator.js" defer></script>
</head>

<body>
  <div class="calculator-grid">
    <div class="output">
      <div data-previous-operand class="previous-operand"></div>
      <div data-current-operand class="current-operand"></div>
    </div>
    <button data-all-clear class="span-two">AC</button>
    <button data-delete>DEL</button>
    <button data-operation>÷</button>
    <button data-number>1</button>
    <button data-number>2</button>
    <button data-number>3</button>
    <button data-operation>*</button>
    <button data-number>4</button>
    <button data-number>5</button>
    <button data-number>6</button>
    <button data-operation>+</button>
    <button data-number>7</button>
    <button data-number>8</button>
    <button data-number>9</button>
    <button data-operation>-</button>
    <button data-number>.</button>
    <button data-number>0</button>
    <button data-equals class="span-two">=</button>
  </div>

</body>

</html>


Solution

  • You do not HAVE this.currentOperandTextElement since it is defined outside the class

    updateDisplay() { 
      currentOperandTextElement.innerHTML = this.currentOperand
    }
    

    Also I changed to delegation

    document.querySelector('.calculator-grid').addEventListener('click', (e) => {
      const tgt = e.target;
      if (tgt.classList.contains('number')) {
        calculator.appendNumber(tgt.textContent)
        calculator.updateDisplay()
      }
    })
    

    All textContents are string so no need for number.toString

    Lastly I do not recommend using data attributes as a placeholder

    This makes more sense

    <button id="all-clear" class="span-two">AC</button>
    <button id="delete">DEL</button>
    <button class="operation">÷</button>
    <button class="number">1</button>
    <button class="number">2</button>
    <button class="number">3</button>
    <button class="operation">*</button>
    <button class="number">4</button>
    <button class="number">5</button>
    <button class="number">6</button>
    <button class="operation">+</button>
    <button class="number">7</button>
    <button class="number">8</button>
    <button class="number">9</button>
    <button class="operation">-</button>
    <button class="number">.</button>
    <button class="number">0</button>
    <button id="equals" class="operation span-two">=</button>
    

    class Calculator {
      constructor(previousOperandTextElement, currentOperandTextElement) {
        this.previousOperandTextElement = previousOperandTextElement
        this.currentOperandTextElement = currentOperandTextElement
        this.currentOperand = ""
        this.clear()
      }
    
      clear() {
        this.previousOperandTextElement = ''
        this.currentOperandTextElement = ''
        this.operation = undefined
      }
    
      delete() {
    
      }
    
      appendNumber(number) {
        this.currentOperand  += number
      }
    
      chooseOperator(operation) {
    
      }
    
      compute() {
    
      }
    
      updateDisplay() { 
        currentOperandTextElement.innerHTML = this.currentOperand
      }
    
    
    }
    
    const equalsButton = document.getElementById('equals')
    const deleteButton = document.getElementById('delete')
    const allClearButton = document.getElementById('all-clear')
    const previousOperandTextElement = document.querySelector('[data-previous-operand]')
    const currentOperandTextElement = document.querySelector('[data-current-operand]')
    
    const calculator = new Calculator(previousOperandTextElement, currentOperandTextElement)
    
    document.querySelector('.calculator-grid').addEventListener('click', (e) => {
      const tgt = e.target;
      if (tgt.classList.contains('number')) {
        calculator.appendNumber(tgt.textContent)
        calculator.updateDisplay()
      }
    })
    *,
    *::before,
    *::after {
      box-sizing: border-box;
      font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
      font-weight: normal;
    }
    
    body {
      padding: 0;
      margin: 0;
      background: linear-gradient(to right, #00AAFF, #00FF6C);
    }
    
    .calculator-grid {
      display: grid;
      justify-content: center;
      align-content: center;
      min-height: 100vh;
      grid-template-columns: repeat(4, 100px);
      grid-template-rows: minmax(120px, auto) repeat(5, 100px);
    }
    
    .calculator-grid button {
      cursor: pointer;
      font-size: 2rem;
      border: 1px solid white;
      outline: none;
      background-color: rgba(255, 255, 255, .75);
    }
    
    .calculator-grid button:hover {
      background-color: rgba(255, 255, 255, .9);
    }
    
    .span-two {
      grid-column: span 2;
    }
    
    .output {
      grid-column: 1 / -1;
      background-color: rgba(0, 0, 0, 0.75);
      border: solid 5px green;
      display: flex;
      align-items: flex-end;
      flex-direction: column;
      justify-content: space-between;
      padding: 15px;
    }
    
    .output .previous-operand {
      color: rgba(255, 255, 255, .75);
      font-size: 1.5rem;
    }
    
    .output .current-operand {
      color: rgba(255, 255, 255, .75);
      font-size: 2.5rem;
    }
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Calculator</title>
      <link rel="stylesheet" href="calculator.css">
      <script src="calculator.js" defer></script>
    </head>
    
    <body>
      <div class="calculator-grid">
        <div class="output">
          <div data-previous-operand class="previous-operand"></div>
          <div data-current-operand class="current-operand"></div>
        </div>
        <button id="all-clear" class="span-two">AC</button>
        <button id="delete">DEL</button>
        <button class="operation">÷</button>
        <button class="number">1</button>
        <button class="number">2</button>
        <button class="number">3</button>
        <button class="operation">*</button>
        <button class="number">4</button>
        <button class="number">5</button>
        <button class="number">6</button>
        <button class="operation">+</button>
        <button class="number">7</button>
        <button class="number">8</button>
        <button class="number">9</button>
        <button class="operation">-</button>
        <button class="number">.</button>
        <button class="number">0</button>
        <button id="equals" class="operation span-two">=</button>
      </div>
    
    </body>
    
    </html>