Jay Jenkins
Jay Jenkins

Reputation: 423

Is there a more elegant way to write these conditions?

    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

Upvotes: 3

Views: 98

Answers (6)

Slai
Slai

Reputation: 22886

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 )

Upvotes: 4

OliverRadini
OliverRadini

Reputation: 6466

Here are a few ways to approach the problem if you were willing to use a function to determine the maximum difference between an array of numbers. The shortest if probably the neatest, just a few ideas on how you may approach the problem:

const shortGetMaxDifference = numbers => Math.max(...numbers) - Math.min(...numbers);

const getMaxDifference = numbers => {
  const sorted = numbers
    .sort((a, b) => a > b ? -1 : 1)
    
  return sorted[0] - sorted[sorted.length-1]
}

const longWindedGetMaxDifference = numbers => numbers
  .map(i => numbers.map(j => Math.max(i, j) - Math.min(i, j))
  .sort((a, b) => a > b ? -1 : 1))
  .reduce((prev, curr) => [...prev, ...curr], [])
  .sort((a, b) => a > b ? -1 : 1)[0]
  
const numbers = [1, 4, -5, 7, 13]

console.log(shortGetMaxDifference(numbers))
console.log(getMaxDifference(numbers))
console.log(longWindedGetMaxDifference(numbers))

const first = 1;
const second = 3;
const third = 2;

if (getMaxDifference([first, second, third]) >= 2) {
  console.log('The difference is bigger');
}

Upvotes: 1

charlietfl
charlietfl

Reputation: 171690

Throw the subtraction expressions in an array and use Array#some() and Math.abs

if ( [first - second, first - third , second - third].some(n => Math.abs(n) >=2 ) )

Upvotes: 3

Samuel Hulla
Samuel Hulla

Reputation: 7109

One (objectively) more elegant way is to utilize the Math.abs() function:

if (Math.abs(first-second) >= 2 || Math.abs(first-third) >= 2 || Math.abs(second-third) >= 2)){
    // do something
}

A subjective (and completely unnecessary I might add) way to make it aesthetically more pleasing would be:

function plusminus2(val1, val2){
   return ((Math.abs(val1 - val2) >= 2) ? true : false);
}

if (plusminus2(first, second) || plusminus2(first, third) || plusminus2(second, third)){
  // do something
}

But I generally would not recommend doing something like this. While it may save you a couple of seconds writing some if statements (presuming there's more than this single one) it's going to cause an absolute headache anyone else trying to comprehend your code or even yourself if you open it 4 years down the line!

Upvotes: 0

Tilak Lodha
Tilak Lodha

Reputation: 1

Math.abs() will help you, you can break the several conditions to only 3 conditions.

Math.abs(first - second) >= 2 and similarly others

Upvotes: 0

Faical ARAB
Faical ARAB

Reputation: 385

You can breakdown theif statement to 3 conditions simply bt using Math.abs(). Here's is it :

if( Math.abs( first - second ) >= 2 || Math.abs( first - third ) >= 2 || Math.abs( second - third ) >= 2 ) 

Math.abs() function returns the absolute value of it's argument.

Upvotes: 2

Related Questions