Basic JavaScript - Use Multiple Conditional (Ternary) Operators

I wrote this code and it will not work but i am convinced this is correct? Can someone please enlighten me?

return (num > 0) ? “positive” : (num < 0) ? “negative” : (num === 0) ? “zero”;

Thanks in advance,
Hassan

What error message do you get?

Hi, when i click on “Run all tests” nothing happens … the only error i notice is the last “}” has a red underline…

I think the best way to approach this problem is to follow the format of the example given on the left hand side of the screen.

My understanding of it (and please someone feel free to call me out on this because I’m not 100% sure) is that in your format you are essentially saying:

IF (num > 0) { return “positive”};
ELSE IF (num < 0) { return “negative”};
ELSE IF (num ===0) { return “zero”};

and with conditional operators this will not work because you don’t have a final ELSE statement. You therefore need to find a way to turn your last IF/ELSE statement into just an ELSE

3 Likes

it worked thank you so much! I changed the code to what you said…

return (num > 0) ? “positive” : (num < 0) ? “negative” : “zero”;

This is my first time posting here and with community responding it made me understand my problems better.

Thanks zdflower and hogues.uk ^_^.

4 Likes

I think it could work this way:

return (num > 0 ? "positive" : (num < 0 ? "negative" : (num === 0 ? "zero" : "not possible")));

And you need both the result for the true branch and for the false branch. That’s why the “not possible”.

1 Like

You are not returning completely. This is one situation where a code formatter would help you debug this.

I think you can debug this yourself. Go to Prettier and type in your statement word by word. Don’t paste it in because it’ll blow the error trace. Do that and it’ll give you a hint on what’s going on.

1 Like

Wow this is awesome thanks for telling me about the website. I am going to paste so much code into here :slight_smile: so far it did not like " but preferred single like this ’ haha …

1 Like

I should have mentioned this –

This site is for formatting code correctly, with minor options. Quotes and semi-colons can be toggled on the bottom left button, show options. These options are personal and don’t affect how code runs.

Also, if you use an editor like VScode, you can install this plugin.

I got stuck on this one for a while, until I enclosed the curly braces } after checkSign(10) and with a semi-colon in there, which isn’t in the original code so assumed it wasn’t needed - I tried a few solutions here - but none worked - it was only after doing these things mentioned above did it finally pass:

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

checkSign(10);}

function checkSign(num) {

return (num == 0) ? “zero” :

(num > 0) ? “positive” : “negative”:

checkSign(10);}

1 Like

Hi, I’m not sure if this issue is still active or not but i solved the problem as below:

return num > 0 ? “positive” : (num < 0) ? “negative” : (num === 0)? “zero” : " ";

return (num === 0) ? "zero" : (num > 0 ) ? "positive" : "negative";

@freecodecamp-team:
The description for this Challenge is not clear enough!

You need to tell the user, that the “STRING” “positive”, “negative” , “zero” should be returned.

BE SPECIFIC, because it’s the daily bread for a Developer to be!

just my 2 cents but don’t use multiple ternary operators inline please. you may save a few lines of code but it makes an unreadable mess for anyone who should have to maintain your code.

^ Please listen to this suggestion.

1 Like

Multiple ternary operators are actually pretty elegant if you lay them out like a table:

noise = "dog"   ? "bark"
      : "cat"   ? "meow"
      : "horse" ? "neigh"
      : "bird"  ? "chirp"
      : ""

Just don’t try this in PHP, which gets the associativity wrong

I was very close, but stuck on this one for a while. Looked up your code and finally got it right ! thanks