5

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!

Pavel T
  • 79
  • 5

6 Answers6

5

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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
4
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;
jaredrethman
  • 512
  • 5
  • 16
4

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
Salman A
  • 262,204
  • 82
  • 430
  • 521
4

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)

adiga
  • 34,372
  • 9
  • 61
  • 83
  • You're welcome. You can [mark it as accepted](https://stackoverflow.com/help/accepted-answer) by clicking on the grey checkmark on the left – adiga Mar 08 '19 at 20:09
2

Something like this,

baz = foo > bar ? foo - bar : foo + bar
will92
  • 956
  • 7
  • 12
1

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.

Nick
  • 1,392
  • 1
  • 13
  • 20