Multiple Conditional Operators Question

Tell us what’s happening:
I can solve the problem when I eliminate the “(num == 0) ?” part of the final condition statement , but why does the code not work the way it is now? For what I have here now, why is the last condition statement not treated essentially as another “else if” statement? Just wondering for future use.


Your code so far

function checkSign(num) {
return (num > 0) ? "positive"
: (num < 0 ) ? "negative"
: (num == 0) ? "zero";


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36.

Challenge: Use Multiple Conditional (Ternary) Operators

Link to the challenge:

This is more if/else situation where both true and false expressions are required rather than else if. In challenge second condition is just nested. Currently the lack of false expression for the last condition results in syntax error.

Hello, Lukedoc.

The reason is essentially the same as why the operators are called Ternary Operators.

The phrase is a set of three. This: (num == 0) ? "zero" is not a set of three. Once you put the ? operator, the compiler expects there to be an option if true and an option if false. That is why you could replace the above statement with this:

(num === 0) && "zero"

This will give you the result you want.

Granted, you can have a ternary operator phrase look like this:

(num > 0) ? "positive" : (num < 0 ) ? "negative" : "zero"

Which appears more than three, but this can be written to be the same:

(num > 0) ? "positive" : do_something(num)

function do_something(num) {
  return (num < 0) ? "negative" : "zero"

Which is 2 sets of three.

Hope this helps