I got an problem i’d like to solve.
This expression works as long as x is positive
x > 0 ? x - y : x + y;
When it gets negative however this is where the problem begins.
So i thought of
x !=0 & x < 0 ? x - y : Math.abs(x - y)*-1)
but this gets me nowhere and i solved it by:
if (x > 0 && x - y > 0) {
x -= y;
} else if (x < 0 && x + y > 0) {
x += y;
} else {
x = 0;
}
this however is long and appears not elegant.
any way to make this shorter and efficient with ternary operations?
or