Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional operator for an operator

I have a piece of code:

if (foo > bar) {
    baz = foo - bar
} else {
    baz = foo + bar
}

I have a question if I can somehow shorten this code to a single line, something like

PSEUDOCODE:

baz = foo (foo > bar ? + : -) bar

Real code I'd like to shorten

if (gradientStartH > gradientEndH) {
    h = gradientStartH - Math.abs(gradientStartH - gradientEndH) / arr.length * i
} else {
    h = gradientStartH + Math.abs(gradientStartH - gradientEndH) / arr.length * i
}

Thanks!

like image 552
Pavel T Avatar asked Mar 08 '19 19:03

Pavel T


People also ask

Which type of operator is a conditional operator?

The conditional operator (? :) is a ternary operator (it takes three operands).

What are the 3 conditional operators?

There are three conditional operators: && the logical AND operator. || the logical OR operator. ?: the ternary operator.

How do you write a conditional operator?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

What is a conditional operator expression?

The conditional operator ?: , also known as the ternary conditional operator, evaluates a Boolean expression and returns the result of one of the two expressions, depending on whether the Boolean expression evaluates to true or false , as the following example shows: C# Copy. Run.


3 Answers

You could convert the check to a number or take -1 as factor.

baz = foo + (foo > bar || -1) * bar

The cleanest approach is to use an object with the operands and a check for getting the operands.

op = {
    true: function (a, b) { return a + b; }, // add
    false: function (a, b) { return a - b; } // sub
}
baz = op[foo > bar](foo, bar);
like image 108
Nina Scholz Avatar answered Oct 20 '22 23:10

Nina Scholz


You can remove the if-else and Math.abs to just this:

h = gradientStartH - (gradientStartH - gradientEndH) / arr.length * i

Here's a snippet comparing it with your code:

// Your code 
function getDiffExisting(gradientStartH, gradientEndH, arr) {
  let h = 0;
  
  if (gradientStartH > gradientEndH) {
    h = gradientStartH - Math.abs(gradientStartH - gradientEndH) / arr.length
  } else {
    h = gradientStartH + Math.abs(gradientStartH - gradientEndH) / arr.length
  }
  
  return h;
}

console.log(getDiffExisting(200, 100, [1,2]))
console.log(getDiffExisting(50, 100, [1,2]))

function getDiffNew(gradientStartH, gradientEndH, arr) {
  let h = gradientStartH - (gradientStartH - gradientEndH) / arr.length
  return h;
}

console.log(getDiffNew(200, 100, [1,2]))
console.log(getDiffNew(50, 100, [1,2]))

(I have removed the i for testing purposes)

like image 4
adiga Avatar answered Oct 20 '22 23:10

adiga


baz = foo > bar ? foo - bar : foo + bar;

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

EDIT:

I understand your specific question. I'm pretty sure I'm going to get lynched for saying this, but you could use eval() to evaluate it as a string. Not recommend, if any of the below derives via unsantized user data.

h = eval(`${gradientStartH} ${gradientStartH > gradientEndH ? '-' : '+'} ${Math.abs(gradientStartH - gradientEndH) / arr.length * i}`);

Otherwise a decent two liner. Preferred.

const absVal = Math.abs(gradientStartH - gradientEndH) / arr.length * i;
h = gradientStartH > gradientEndH ? gradientStartH - absVal : gradientStartH + absVal;
like image 4
Mr Rethman Avatar answered Oct 20 '22 23:10

Mr Rethman