0
0
Rubyprogramming~10 mins

Ternary operator in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assign 'adult' if age is 18 or more, otherwise 'minor'.

Ruby
status = age [1] 18 ? 'adult' : 'minor'
Drag options to blanks, or click blank then click option'
A!=
B>=
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>=' causes wrong classification.
Using '==' only checks for exactly 18, not adults older than 18.
2fill in blank
medium

Complete the code to assign 'even' if number is divisible by 2, else 'odd'.

Ruby
result = number.[1](2) == 0 ? 'even' : 'odd'
Drag options to blanks, or click blank then click option'
Adiv
Bmod
Cremainder
Dmodulo
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mod' or 'modulo' which are not Ruby methods.
Using 'div' which returns quotient, not remainder.
3fill in blank
hard

Fix the error in the ternary operator to assign 'positive' if num > 0, else 'non-positive'.

Ruby
label = num [1] 0 ? 'positive' : 'non-positive'
Drag options to blanks, or click blank then click option'
A>
B=>
C<=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=>' which is a hash rocket, not a comparison.
Using '<=' or '==' which do not match the condition.
4fill in blank
hard

Fill both blanks to assign 'high' if score is above 80, else 'low'.

Ruby
level = score [1] 80 ? 'high' : '[2]'
Drag options to blanks, or click blank then click option'
A>
Bhigh
Clow
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' changes the logic.
Assigning 'high' in the else part causes wrong output.
5fill in blank
hard

Fill all three blanks to create a ternary that assigns the uppercase name if active is true, else lowercase.

Ruby
display_name = active ? name.[1] : name.[2].[3]
Drag options to blanks, or click blank then click option'
Aupcase
Bdowncase
Cstrip
Dcapitalize
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'capitalize' instead of 'upcase' or 'downcase'.
Not stripping whitespace in the else part.