-1

I'm writing the code for my html calculator, and I'm stuck at defining its logic. FYI, after having the value a initialized after performing the first operation, the calculator is supposed to retain that value as the variable a and perform an operation with the new b, which is input after one of the operator button has been pressed.

However, at the time of this writing, this doesn't work. FYI, after I perform 14+3=17, I press "-" and "5". After finalizing with "=", I got -2. I thought that after a has been initialized after aClick() has been executed for the first time, it would start taking the values entered thereafter as 'b'. Does anyone have any idea on why after initializing a, aClick() still acts as if a is undefined?

let a
let b
let operator

function add(a, b) {
  return a + b
}

function subtract(a, b) {
  return a - b
}

function multiply(a, b) {
  return a * b
}

function divide(a, b) {
  return a / b
}

function operate(a, operator, b) {
  switch (operator) {
    case "+":
      return add(a, b)
    case "-":
      return subtract(a, b)
    case "/":
      return divide(a, b)
    case "*":
      return multiply(a, b)
  }
}

numberButtons = document.querySelectorAll('.number')
specialButtons = document.querySelectorAll('.special')
operatorButtons = document.querySelectorAll('.operator')
resetButton = document.getElementById('C')
resultButton = document.getElementById('=')

function addNumber(Event){
    document.getElementsByClassName('up')[0].innerHTML +=Event.target.id
}

function result() {
  resultButton.addEventListener('click', function() {
    b = parseInt(document.getElementsByClassName('up')[0].innerHTML)
    document.getElementsByClassName('down')[0].innerHTML = operate(a, operator, b)
    a = parseInt(document.getElementsByClassName('down')[0].innerHTML)
  })
}


function aClick() {
  if (a === undefined) {
    numberButtons.forEach(button => {
      button.addEventListener("click", addNumber)
    });
    operatorButtons.forEach(button => {
      button.addEventListener('click', function() {
        a = parseInt(document.getElementsByClassName('up')[0].innerHTML)
        operator = button.id
        numberButtons.forEach(element => {
          element.removeEventListener('click', addNumber)
        });
        document.getElementsByClassName('up')[0].innerHTML = ""
        numberButtons.forEach(button => {
          button.addEventListener("click", addNumber)
        });
      })
    });
    result()
  } else if (a !== undefined) {
    operatorButtons.forEach(button => {
      button.addEventListener('click', function() {
        b = parseInt(document.getElementsByClassName('up')[0].innerHTML)
        operator = button.id
        numberButtons.forEach(element => {
          element.removeEventListener('click', addNumber)
        });
        document.getElementsByClassName('up')[0].innerHTML = ""
        numberButtons.forEach(button => {
          button.addEventListener("click", addNumber)
        });
      })
    });
  }
}

aClick()
<div class="display">
  <div class="up"></div>
  <div class="down"></div>
</div>
<div class="button-layout">
  <ul>
    <div>
      <button id="1" class="number">1</button>
      <button id="2" class="number">2</button>
      <button id="3" class="number">3</button>
      <button id="+" class="operator">+</button>
    </div>

    <div>
      <button id="4" class="number">4</button>
      <button id="5" class="number">5</button>
      <button id="6" class="number">6</button>
      <button class="operator" id="-">-</button>
    </div>

    <div>
      <button id="7" class="number">7</button>
      <button id="8" class="number">8</button>
      <button id="9" class="number">9</button>
      <button class="operator" id="*">*</button>
    </div>

    <div>
      <button id="C" class="special">C</button>
      <button id="0" class="number">0</button>
      <button id="=" class="special">=</button>
      <button class="operator" id="/">/</button>
    </div>

  </ul>
</div>
  • Could you show us your code for the `operate` function? You are passing in `a` before getting the value – Thum Choon Tat Aug 22 '23 at 09:30
  • 3
    As I asked [in your previous question](https://stackoverflow.com/questions/76950147/my-html-calculator-display-isnt-reset-after-clicking-an-operator-button) PLEASE create a [mcve] using the `[<>]` snippet editor. Please [edit] the question and provide a running example. – mplungjan Aug 22 '23 at 09:31
  • See if this works [link](https://jsfiddle.net/ViSpace/983u4roa/) – Carl Warren Aug 22 '23 at 09:32
  • @CarlWarren Why would that work? It does not run any code. Just some code and you dropped an `l` from the let. Why not just do as I ask and you will get answers instead of getting downvotes and closed questions – mplungjan Aug 22 '23 at 09:34
  • I've edited my link and my code, so you should be able to run everything now. Tell me if anything is wrong – Carl Warren Aug 22 '23 at 09:41
  • I made you the snippet I asked for. It has console errors. Please fix (the button collections are not defined) – mplungjan Aug 22 '23 at 12:15
  • Here is an update on your previous question which shows a working calculator using your calculate without eval https://stackoverflow.com/a/76950659/295783 – mplungjan Aug 22 '23 at 13:00
  • I have checked and edited the code. Now you should be able to run the snippet – Carl Warren Aug 23 '23 at 07:39

0 Answers0