0
0
Rubyprogramming~10 mins

Conditional assignment (||=) 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 10 to x only if x is nil or false.

Ruby
x [1] 10
Drag options to blanks, or click blank then click option'
A+=
B=
C&&=
D||=
Attempts:
3 left
💡 Hint
Common Mistakes
Using = assigns unconditionally.
Using &&= assigns only if variable is truthy.
Using += adds to the existing value.
2fill in blank
medium

Complete the code to set default value 'guest' for username if it is nil or false.

Ruby
username [1] 'guest'
Drag options to blanks, or click blank then click option'
A&&=
B=
C||=
D+=
Attempts:
3 left
💡 Hint
Common Mistakes
Using = overwrites the value always.
Using &&= assigns only if variable is truthy.
Using += tries to add strings.
3fill in blank
hard

Fix the error in the code to assign 5 to count only if count is nil or false.

Ruby
count [1] 5
Drag options to blanks, or click blank then click option'
A||=
B=
C&&=
D+=
Attempts:
3 left
💡 Hint
Common Mistakes
Using = assigns unconditionally.
Using &&= assigns only if variable is truthy.
Using += adds to the existing value.
4fill in blank
hard

Fill both blanks to assign 'blue' to color only if color is nil or false, and then append ' sky' to it.

Ruby
color [1] 'blue'
color [2] ' sky'
Drag options to blanks, or click blank then click option'
A||=
B+=
C=
D&&=
Attempts:
3 left
💡 Hint
Common Mistakes
Using = instead of ||= causes unconditional assignment.
Using &&= instead of ||= won't assign if nil.
Using = instead of += won't append.
5fill in blank
hard

Fill all three blanks to assign 0 to score if nil or false, then add 10, then assign 'done' to status if nil or false.

Ruby
score [1] 0
score [2] 10
status [3] 'done'
Drag options to blanks, or click blank then click option'
A||=
B+=
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using = instead of ||= causes unconditional assignment.
Using = instead of += won't add to score.
Mixing up the order of operations.