Conditional operator for an operator
Asked Answered
R

6

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!

Rowlett answered 8/3, 2019 at 19:24 Comment(3)
If the operation is "much more complicated", a ternary should be avoided, for readability and maintainability reasonsSobranje
baz = (foo > bar) ? (foo - bar) : (foo + bar) ?Nuzzi
@Sobranje maybe you are right! But please take a look on real code example that I'd like to "enhance"Rowlett
D
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;
Darbydarce answered 8/3, 2019 at 19:26 Comment(0)
P
4

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);
Planetoid answered 8/3, 2019 at 19:28 Comment(1)
You probably mean object i guess there's a typo in your answer, +1 nice answerQuinquepartite
S
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
Suttles answered 8/3, 2019 at 19:58 Comment(0)
E
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)

Englut answered 8/3, 2019 at 19:59 Comment(1)
You're welcome. You can mark it as accepted by clicking on the grey checkmark on the leftEnglut
N
2

Something like this,

baz = foo > bar ? foo - bar : foo + bar
Nahshunn answered 8/3, 2019 at 19:26 Comment(0)
K
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.

Kerch answered 8/3, 2019 at 19:29 Comment(1)
That was a pseudo code of how it would be look like so I could better explain myselfRowlett

© 2022 - 2025 — McMap. All rights reserved.