chibiw3n
chibiw3n

Reputation: 357

Create a calculator with Javascript - Not sure why the display is not updating

I am currently trying to learn javascript by creating a calculator and following a youtube tutorial. I am trying to make a number appear on the display when a number button is clicked on, however nothing seems to be happening when I click the buttons. I added an Event Listener to each of the number buttons so that when they are pressed, the innerText of the display should update to the number clicked.

class Calculator {
  constructor(previousOperandTextElement, currentOperandTextElement) {
    this.previousOperandTextElement = previousOperandTextElement;
    this.currentOperandTextElement = currentOperandTextElement;
    this.clear();
  }
  clear() {
    this.previousOperandElement = '';
    this.currentOperandElement = '';
    this.operation = undefined;
  }

  delete() {

  }

  appendNumber(number) {
    this.currentOperand = number;
  }

  chooseOperation(operation) {

  }

  compute() {

  }

  updateDisplay() {
    this.currentOperandTextElement.innerText = 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(currentOperandTextElement, previousOperandTextElement);

numberButtons.forEach(button => {
  button.addEventListener('click', () => {
    calculator.appendNumber(button.innerText);
    calculator.updateDisplay();
  });
});
* {
  font-family: Gotham Rounded, sans-serif;
}

body {
  margin: 0;
  padding: 0;
  background-color: gray;
}

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

.calculator-grid button {
  cursor: pointer;
  font-size: 2rem;
  background-color: white;
  opacity: 0.75;
  outline: none;
  border: 1px solid white;
}

.calculator-grid button:hover {
  background-color: white;
  opacity: 0.9;
}

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

.display {
  grid-column: 1 / -1;
  background-color: black;
  opacity: 0.75;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  padding: 10px;
  align-items: flex-end;
  color: white;
}

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

.current-operand {
  color: white;
  font-size: 2rem;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=display-width, intial-scale=1.0">
  <title>Trial</title>
  <link rel="stylesheet" href="styles.css">
</head>

<body>
  <div class="calculator">
    <div data-all-clear class="calculator-grid">
      <div class="display">
        <div data-previous-operand class="previous-operand"></div>
        <div data-current-operand class="current-operand"></div>
      </div>
      <button data-delete class="span-two">AC</button>
      <button data-operation="">DEL</button>
      <button data-operation="">÷</button>
      <button data-number>1</button>
      <button data-number>2</button>
      <button data-number>3</button>
      <button>*</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>
  </div>
</body>
<script src="script.js"></script>

</html>

Upvotes: 0

Views: 95

Answers (2)

Anya Schechter
Anya Schechter

Reputation: 36

Try replacing your numberButtons variable with this:

const numberButtons = document.querySelectorAll('[data-number]');

You've made some typos, quotes should be outside the square brackets, not inside.

Upvotes: 0

Sascha A.
Sascha A.

Reputation: 4626

You set the brackets outside of the quotationmarks at the querySelector:

document.querySelector('[data-equals]');

Now your number is displayed.

class Calculator{
    constructor(previousOperandTextElement, currentOperandTextElement){
        this.previousOperandTextElement = previousOperandTextElement;
        this.currentOperandTextElement = currentOperandTextElement;
        this.clear();
    }
    clear(){
        this.previousOperandElement = '';
        this.currentOperandElement = '';
        this.operation = undefined;
    }
    
    delete(){

    }

    appendNumber(number){
        this.currentOperand = number;
    }

    chooseOperation(operation){

    }

    compute(){

    }

    updateDisplay(){
        this.currentOperandTextElement.innerText = 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(currentOperandTextElement, previousOperandTextElement);

numberButtons.forEach(button => {
    button.addEventListener('click', () => {
        calculator.appendNumber(button.innerText);
        calculator.updateDisplay();
    });
});
*{
    font-family: Gotham Rounded, sans-serif;
}

body{
    margin: 0;
    padding: 0;
    background-color: gray;
}

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

.calculator-grid button{
    cursor: pointer;
    font-size: 2rem;
    background-color: white;
    opacity: 0.75;
    outline:none;
    border: 1px solid white;
}

.calculator-grid button:hover{
    background-color: white;
    opacity: 0.9;
}

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

.display{
    grid-column: 1 / -1;
    background-color: black;
    opacity: 0.75;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    padding: 10px;
    align-items: flex-end;
    color: white;
}
 
.previous-operand{
    color: rgba(255, 255, 255, 0.7);
    font-size: 1.5rem;
}

.current-operand{
    color: white;
    font-size: 2rem;
}
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=display-width, intial-scale=1.0">
        <title>Trial</title>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <div class="calculator">
            <div data-all-clear class="calculator-grid">
                <div class="display">
                    <div data-previous-operand class="previous-operand"></div>
                    <div data-current-operand class="current-operand"></div>
                </div>
                <button data-delete class="span-two">AC</button>
                <button data-operation="">DEL</button>
                <button data-operation="">÷</button>
                <button data-number>1</button>
                <button data-number>2</button>
                <button data-number>3</button>
                <button>*</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>
        </div>
    </body>
</html>

Upvotes: 3

Related Questions