0
0
Rubyprogramming~10 mins

Ternary operator usage 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 'even' if number is divisible by 2, otherwise 'odd'.

Ruby
result = number % 2 == 0 ? [1] : 'odd'
Drag options to blanks, or click blank then click option'
A'even'
B'odd'
C'zero'
D'none'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting 'odd' before the colon instead of 'even'.
Forgetting quotes around the string.
2fill in blank
medium

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

Ruby
status = age >= 18 ? [1] : 'minor'
Drag options to blanks, or click blank then click option'
A'child'
B'adult'
C'teen'
D'senior'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'child' or 'teen' instead of 'adult'.
Confusing the true and false parts.
3fill in blank
hard

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

Ruby
label = num > 0 ? [1] : 'non-positive'
Drag options to blanks, or click blank then click option'
Atrue
Bpositive
Cnum
D'positive'
Attempts:
3 left
💡 Hint
Common Mistakes
Missing quotes around 'positive'.
Using a variable name instead of a string.
4fill in blank
hard

Fill both blanks to assign 'even' if n is divisible by 2, else 'odd'.

Ruby
result = n % 2 [1] 0 ? 'even' : [2]
Drag options to blanks, or click blank then click option'
A==
B!=
C'odd'
D'even'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '=='.
Swapping 'even' and 'odd' in the false part.
5fill in blank
hard

Fill all three blanks to assign 'positive', 'negative', or 'zero' based on the value of val.

Ruby
label = val > 0 ? [1] : val < 0 ? [2] : [3]
Drag options to blanks, or click blank then click option'
A'positive'
B'negative'
C'zero'
D'none'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of conditions.
Forgetting quotes around strings.