Use Multiple Conditional (Ternary) Operators_help

Tell us what’s happening:
I need help on what I did wrong.

Your code so far


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

checkSign(10);

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 12105.100.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.144 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/use-multiple-conditional-ternary-operators/

(num > 0 && num < 0)
Can you explain what this code is doing?

check if the number is greater than or less than zero:sweat:

&& is “and”. A number cannot be greater than zero AND less than zero.
|| is “or”. The only number that is not greater than zero OR less than zero is… zero. And this won’t be executed if num is zero, because you’ve already tested for that.

You’ll want to be able to easily translate back and forth between the ternary and a regular if/else block. How does your current code translate into an if/else chain?

1 Like

ah i understand now, if i put (num > 0) i would have to first check if is true which would make the first condition positive and then if its not then it would be negative.

Is this a accurate explanation?

*negative as the second condition

Yup.

(thingThatIsTrueOrFalse) ? thingToDoIfTrue : thingToDoIfFalse
1 Like

Thanks for the insight.

I’m glad I could help. Happy coding!