0

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/

Sam Jefferies
  • 584
  • 1
  • 7
  • 27
  • Does this answer your question? [What's the prettiest way to compare one value against multiple values?](https://stackoverflow.com/questions/9121395/whats-the-prettiest-way-to-compare-one-value-against-multiple-values) – pilchard Jun 04 '22 at 12:47
  • which itself is a duplicate linking to [Check variable equality against a list of values](https://stackoverflow.com/questions/4728144/check-variable-equality-against-a-list-of-values) – pilchard Jun 04 '22 at 12:48
  • Thinking about the priority of the operators in your test: if (pressedKey === "+" || "-" || "*" || "Enter" || "Backspace" || "." || "/") is equivalent to if ((pressedKey === "+") || "-") as the first test gives false (if the key is no +) so the system moves onto the next value which is non zero (it is "-") so not false. The next ORs are not evaluated. The condition is always true. – A Haworth Jun 04 '22 at 13:15

3 Answers3

2

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");
  }
};
Heet Vakharia
  • 405
  • 4
  • 14
1

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 === "/")
...
Zach Jensz
  • 3,650
  • 5
  • 15
  • 30
markcalendario
  • 218
  • 2
  • 9
1

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

Andy
  • 61,948
  • 13
  • 68
  • 95
  • 12 year old duplicate. – pilchard Jun 04 '22 at 13:07
  • While I agree with you, that dupe needs _a lot_ of curating. There are answers there spanning an entire 12 years, and is confusing, and JS has moved on. Sometimes it's easier to create a new answer that belongs in this decade. @pilchard – Andy Jun 04 '22 at 13:19
  • Hmmm, [answer with second most votes](https://stackoverflow.com/a/40910577/13762301) `if ([].includes()) { …` pretty confusing. – pilchard Jun 04 '22 at 14:34