0

im doing a basic calculator to learn JS. Right now im making an if condition that if the button pressed is NOT a number, then do something. While i was writing it i wondered if there's a way of doing something like this (of course THIS exact sintaxys does not exist, i know that, but I think you'll understand what im looking for)

if (buttonPressed.innerText != "1,2,3,4,5,6,7,8,9,0")

or anything similar, so i can avoid doing writing:

if (buttonPressed.innerText != "1" && buttonPressed.innerText != "2" && buttonPressed.innerText != "3" , etc)

basically, an easier and faster way of writing multiple conditions to avoid such an extensive text. i tried googling this but since Spanish is my main language and not English i dont know how to formulate this question briefly to work on google. Thanks in advance :)

  • `if (!"1,2,3,4,5,6,7,8,9,0".includes(buttonPressed.innerText))` [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) –  Aug 02 '22 at 21:44
  • @ChrisG What are the commas for in this case? It’s just checking for a digit. – Dave Newton Aug 02 '22 at 21:55
  • @DaveNewton True but I thought I'd use OP's string just to show how close they were. –  Aug 02 '22 at 21:58
  • also, how would you get a two or three digit number without them – DCR Aug 02 '22 at 22:27

0 Answers0