Skip to content

handleKeyPress(event) sometimes malfunctions #24

@josueJURE

Description

@josueJURE

I created the function below to meet accessibility requirements. When I first open the app and enter "1+1" and then press "=" I get "21" instead of "2". The calculator behave as expected after refreshing the page. Still it makes for a bad user experience.

function handleKeyPress(event) {
  const key = event.key;
  const button = document.querySelector(`[data-value="${key}"]`);
  console.log(key)
  if (button) {
    button.click(); // Trigger the click event for the corresponding button
  } 
  if (event.code === "Backspace") {
    let newArray = data.slice(ZERO, -1);
    screen.innerText = newArray.join("");
    data = newArray;
    if (screen.innerText === "") {
      screen.innerText = ZERO;
    }
  }
  if(event.code === "Enter") {
    userClicksOnEqualButton("=");
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions