#Condition Operators Quiz
Objectives
- Test your understanding of the ternary operator
- Understand when to use the ternary operator
- Understand that use of statement modifiers (trailing if and trailing unless statements)
- Understand the use of trailing unless statement
???
Quiz
?: Which of the following if
statements is a good candidate for using a ternary operator instead?
(X)
if condition_a
something
else
something_else
end
( )
if condition_a
something
elsif condition_b
something_else
else
something_even_more_different
end
?: Let's convert the correct answer above using a ternary operator. Which of the following is the correct way to do that?
( )
condition_a ? something : condition_b
( )
condition_a ? something : (conditional_b ?something_else : something_even_more_different)
(X)
condition_a ? something : something_else
( )
condition_a ? something_else : something
?: When is it a good idea to use an if
conditional over a ternary?
( ) When the if
statement is complex with multiple elseif
conditions
( ) When it would require a nested ternary statement
( ) When the ternary statement takes up more than one line
(X) All of the above
?: What will this code print out:
name = "Steven"
puts "Hi, #{name}" if name == "Steven"
( ) "Hi, #{name}" (X) "Hi, Steven" ( ) Nothing
?: What will this code print out:
name = "Steven"
puts "Hi, #{name}" unless name == "Steven"
( ) "Hi, #{name}" ( ) "Hi, Steven" (X) Nothing
?: If you are unsure whether to use the ternary operator, or the if statement, which should you use?
(X) if
statement
( ) ternary statement
???
View Condition Operators Quiz on Learn.co and start learning to code for free.