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!
The conditional operator (? :) is a ternary operator (it takes three operands).
There are three conditional operators: && the logical AND operator. || the logical OR operator. ?: the ternary 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.
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.
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);
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)
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With