Pavel T
Pavel T

Reputation: 97

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!

Upvotes: 5

Views: 138

Answers (6)

Salman Arshad
Salman Arshad

Reputation: 272106

You were almost there. foo - bar could be written as foo + -bar. So the pseudo code could be written as:

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

Upvotes: 4

jaredrethman
jaredrethman

Reputation: 511

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;

Upvotes: 4

adiga
adiga

Reputation: 35222

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)

Upvotes: 4

Nina Scholz
Nina Scholz

Reputation: 386578

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);

Upvotes: 4

Nick
Nick

Reputation: 1422

You could use a ternary operator, as you attempted in your original answer. The syntax for a ternary operator should be condition ? true : false;

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

You mentioned that it "might be much more complicated than this" which does not allow much clarity for a solution. Ternary operators likely will not work depending on the complexity. You can read more about ternary operators here.

Upvotes: 1

will92
will92

Reputation: 1044

Something like this,

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

Upvotes: 2

Related Questions