1

I have a code snippet below which i'm trying to find if a field has a value of 90 or not. I've tried it with only the one field and it works fine but when I add the other two in with ORs all of them don't work:

The working script:

if (number1 === "90") {
    message = "Right angle";
} 

The not working script:

if (number1 || number2 || number3 === "90") {
    message = "Right angle";
} 

On the bottom code snippet the term "right angle" is returned for any value

ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89
busyguy
  • 207
  • 3
  • 7
  • if `number1` is actual a number ,then don't compare to a string – Carsten Løvbo Andersen Mar 04 '22 at 11:06
  • 2
    You can't short-circuit comparisons like that. Your expression is evaluated as `(number1) || (number2) || (number3 === "90")` –  Mar 04 '22 at 11:07
  • Pro-tip `number1 == 90` will also account for it being the string representation – Andrew Corrigan Mar 04 '22 at 11:11
  • To follow on the above comment: so it should be `if (number1==="90" || number2==="90" || number3==="90")` – freedomn-m Mar 04 '22 at 11:11
  • `if( [number1, number2, number3].includes("90") ) { .. }` – adiga Mar 04 '22 at 11:18
  • @AndrewCorrigan that's not a pro-tip, it's a noob one. A pro would ensure that the variable is explicitly coerced into the expected type _before_ comparison. It's the only way to eliminate the WAT?! factor. – Alnitak Mar 04 '22 at 11:21
  • 1
    @Alnitak, I know that, you know that. I just didn't know how to phrase it without being insulting; so I pretended it was a pro-tip – Andrew Corrigan Mar 04 '22 at 11:24
  • Though unrelated, I'd say: noob: use `==`, junior pro: use `===`, seasoned pro: I use `==` because I know what I'm doing, pro-pro: use `===` cause someone else might see your code that doesn't know what they're doing! – freedomn-m Mar 04 '22 at 11:47

0 Answers0