Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
OR statement being ignored

I'm trying to fire my masterFunction only if certain keys are pressed. However it's being called regardless of what button is clicked. Even though the console log shows a different key.

Can anyone advise?

//Update display on keydown event
addEventListener('keydown', (e) => {
    pressedKey = event.key;
    console.log(pressedKey);
    keyDownOperatorMapping(e);
});

let keyDownOperatorMapping = (e) => {
if (pressedKey === "+" || "-" || "*" || "Enter" || "Backspace" || "." || "/") {
masterFunction(e);
} 
};

Example: https://jsfiddle.net/rykawgs9/

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

It is because your current condition is

Hey is pressedKey equal is "+" or is "-" is true or //goes on

As string with length more than one is always one it always return true your condition is always true

So one way archive these

addEventListener('keydown', (e) => {
  pressedKey = event.key;
  console.log(pressedKey);
  keyDownOperatorMapping(e);
});

let keyDownOperatorMapping = (e) => {
  const keys = ["+", "-", "*", "Enter", "Backspace", ".", "/"]
  if (keys.includes(pressedKey)) {
    console.log("Found");
  }
};
about 4 years ago · Juan Pablo Isaza Report

0

You have to put the variable that you are comparing to. Which is in your case, the pressedKey:

if (pressedKey === "+" || pressedKey === "-" || pressedKey === "*" || pressedKey === "Enter" || pressedKey === "Backspace" || pressedKey === "." || pressedKey === "/")
...
about 4 years ago · Juan Pablo Isaza Report

0

You can reduce the code a little by adding the searchable keys to an array, and then check that the array includes the key that was entered.

const allowed = ['+', '-', '*', 'Enter', 'Backspace', ',', '/'];

document.addEventListener('keydown', (e) => {

  const { key } = e;

  if (allowed.includes(key)) {

    // masterFunction(e)
    console.log(key);

  }

});

Additional documentation

  • Destructuring assignment
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!