FAQs on the exercise One Good Turn Deserves a Ternary
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
I just started using ternary if/else statements. Im a bit confused why the condition is printed to the command line as 15 (if true) or 16 (if false). Here a short code:
def do_if_true
puts "TRUE!!"
end
def do_if_false
puts "FALSE!!"
end
condition = 3<4
puts condition ? do_if_true : do_if_false
# output in codecademy editor: // TRUE!!
# // 15
# output running in atom: // TRUE!!
EDIT: as pointed out above the output differs depending on where i run the code (webinterface from codecademy vs atom runner). Im just curious why in the codecademy editor i got a second line printing out 15.
ah i just realized that i misunderstood how the ternary syntax works… sorry my code was strange! I was expecting to get the outcome of the condition checking back from the ternary one linerer, and thought that the code after the “?” and after the “:” just gets executed (depending on the condition), whereas whats written there actually is returned! Thanks for your help again!
The returns are on your methods. In Ruby they are implicit. All puts does is print the expression resulting in the ternary. The ternary doesn’t return anything. It’s a control flow device.