 # 16. The Ternary Operator

Hello everyone,
I’m confused in this “level” because i don’t know what to do more , I’ve tried a lot of things and i can’t do this … This is my code:

``````three = 3
puts three == 3 ? "One is les than two!" : "What?"
``````

Thanks,
itsPlaza

are you sure your code doesn’t work? I’ve just tried it and it works just fine.

1 Like

Your code is outputting “One is les than two” not “One is less than two” that is why the tutorial is saying it is wrong. Just fix the spelling and it works.

2 Likes

Hi!

You need to use “1 < 2 ?” instead of “three == 3?” and it will work.
Like this:
puts 1 < 2 ? “One is less than two!” : “One is not less than two.”

Cheers!

4 Likes

Hi guys, just want to know why my code is wrong:

1 < 2 ? “One is less than two!” : "One is not less than two."

It’s interesting for me what will be the difference if I put -

puts 1 < 2 ? “One is less than two!” : “One is not less than two.”

or:

1 < 2 ? “One is less than two!” : “One is not less than two.”

The program is returning the same result.

Thank you

Make sure you remove “end”

you just spelled the word “less” wrong… otherwise it works fine

you can do this in diffrent ways.
here is one, works fine.
one = 1
puts one == 1 ? “One is less than two!” : “One is not less than two.”

Hi there you’re missing an s in less.

Ok so I think I might of done it wrong, even though it passed for me:

numbers = [a, b]
puts numbers << 1 ? “One is less than two!” : “One is not less than two”

I see that I am wrong

dont forget to put a puts in front of the whole thing

The following is my code, and I feel it is right because condition after the “?” (including ?) suffices the if statement. So, we can remove the “if” condition.
puts 1 > 2 ? “One is less than two!” : “One is not less than two.” Ternary operator work flow :
if_this_is_a_true_value ? then_the_result_is_this : else_it_is_this

puts three == 3 ? “One is les than two!” : “What?”

Thats why its return One is les than two!
this is statement not result its modified by user.

Thanks,
Yogesh Waghmare

1 Like