Is there a more elegant way to write these conditions?

  • A+
Category:Languages
    if(first - second >=2 || first - second <=-2 || first - third >=2 || first - third <=-2 || second - third >=2 || second - third <=-2) 

It's disgusting.

I have three values that have to be checked, and if any two of them have a >= 2 difference, then I run through some tasks.

I'm curious, can you suggest a way that would make this more pleasant? Thanks

 


if ( Math.max(first, second, third) - Math.min(first, second, third) >= 2 ) 

or a bit less efficient:

var a = [first, second, third].sort((a, b) => a - b)); if ( a[2] - a[0] >= 2 ) 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: