16. The Ternary Operator


#1

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?"

Someone help me please!
Thanks,
itsPlaza


#2

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


#3

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.


#4

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!


#5

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


#6

Make sure you remove "end"


#7

you just spelled the word "less" wrong... otherwise it works fine


#8

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."


#9

Hi there you're missing an s in less.


#10

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


#11

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


#12

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."
:smiley:


#13

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