Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are ternary operators in ternary operatory possible

Why does this line doesnt work

        x > highNum ? highNum = x : y > highNum ? highNum = y : highNum = highNum

In this case this line is in a loop and x and y is different everytime. I tried to find the highest number at the end and thought this would work. In my mind this reads as: If x is higher than high num highnum should get assigned the value of x if not. is y bigger? if yes y should be the new highnum. if not. dont change high num

like image 346
Venox Avatar asked Jul 31 '21 17:07

Venox


People also ask

Can you put a ternary operator inside a ternary operator?

For example, you should not try to make ternary operators nested inside of ternary operators. Although writing code this way works correctly in JavaScript, it is very hard to read and understand.

Can ternary operator have 3 conditions?

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.

Can ternary operator have two conditions?

In the above syntax, we have tested 2 conditions in a single statement using the ternary operator. In the syntax, if condition1 is incorrect then Expression3 will be executed else if condition1 is correct then the output depends on the condition2. If condition2 is correct, then the output is Expression1.

How can use ternary operator for 3 conditions in C#?

C# - Ternary Operator ?: It is the short form of the if else conditions. The ternary operator starts with a boolean condition. If this condition evaluates to true then it will execute the first statement after ? , otherwise the second statement after : will be executed.


4 Answers

short answer:

Yes, they are.

long answer:

Yes, but you should be concerned about readability too. You code does exactly what you are expecting it to do, but other devs (and maybe you in the future), could have a problem understanding that, so I'd strongly advice you to never use nested ternary operators, and only use them when it makes more sense then a simple if else statement. And if even after all this you still wanna use it, at least add a comment explaining what it does. ex:

  let highNum
  for(let line of lines){
     const {x, y} = line; 
     // use bubble sort to find the highest number
     x > highNum ? highNum = x : y > highNum ? highNum = y : highNum = highNum
  }
  

edit: Also, this is also not correctly finding the highest number, as said by "trincot"

like image 100
Pedro Henrique Avatar answered Oct 16 '22 11:10

Pedro Henrique


There is a potential high value that you could miss: when x > highNum, but also y > x, you will not see that y is really the highest, as the expression will already have decided that highNum should get the value of x.

You can do this quite simple with Math.max:

highNum = Math.max(x, y, highNum);
like image 38
trincot Avatar answered Oct 16 '22 10:10

trincot


Yes, but you'll require brackets, mostly for readability:

(x > highNum) ? (highNum = x) : ((y > highNum) ? (highNum = y) : (highNum = highNum));

In your case, it seems you're better off splitting it into multiple statements to prevent confusion:

if (x > highNum) {
    highNum = x;
} else if (y > highNum) {
    highNum = y;
}

although that doesn't fit in a single expression, but perhaps that's a sign of your code getting a bit too complex/unreadable.

If you're solely looking for the highest number, perhaps Math.max is all you need, i.e. Math.max(x, y, highNum).

like image 3
Kelvin Schoofs Avatar answered Oct 16 '22 10:10

Kelvin Schoofs


If x is higher than high num highnum should get assigned the value of x if not. is y bigger? if yes y should be the new highnum. if not. dont change high num

That would be:

highNum = (x > highNum) ? x : ((y > highNum) ? y : highNum)
           [condition] ?[true]:         [false]
                              ([condition] ? [true] : [false]) // Evaluated if the first condition was false

The structure is variable = [condition] ? [value for true] : [value for false]

And you can have a nested ternary instead of a [value] using additional parenthesis.

This is the same as:

if(x > highNum) {
  highNum = x
} else if(y > highNum) {
  highNum = y
} else {
  highNum = highNum // Quite useless... That is for explanation purpose ;)
}
like image 2
Louys Patrice Bessette Avatar answered Oct 16 '22 11:10

Louys Patrice Bessette