0
0
Rubyprogramming~10 mins

Case/when statement 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 start a case statement for variable grade.

Ruby
case [1]
when 'A'
  puts 'Excellent'
end
Drag options to blanks, or click blank then click option'
Amark
Bscore
Cresult
Dgrade
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not defined or unrelated to the case statement.
2fill in blank
medium

Complete the code to print 'Good' when score is 80.

Ruby
case score
when [1]
  puts 'Good'
end
Drag options to blanks, or click blank then click option'
A80
B70
C'A'
D'B'
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around numbers, which makes them strings and won't match numeric score.
3fill in blank
hard

Fix the error in the case statement to correctly print 'Pass' when result is 'pass'.

Ruby
case result
when [1]
  puts 'Pass'
end
Drag options to blanks, or click blank then click option'
A'pass'
Bpass
C:pass
D"pass"
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted strings which Ruby treats as variables or symbols, causing errors.
4fill in blank
hard

Fill both blanks to complete the case statement that prints 'Low' for scores less than 50 and 'High' for 50 or more.

Ruby
case
when score [1] 50
  puts 'Low'
when score [2] 50
  puts 'High'
end
Drag options to blanks, or click blank then click option'
A<
B>=
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators that reverse the logic.
5fill in blank
hard

Fill all three blanks to create a case statement that prints the day type based on day: 'Weekend' for Saturday or Sunday, 'Weekday' for Monday to Friday, and 'Unknown' otherwise.

Ruby
case day
when [1], [2]
  puts 'Weekend'
when 'Monday'..'Friday'
  puts '[3]'
else
  puts 'Unknown'
end
Drag options to blanks, or click blank then click option'
A'Saturday'
B'Sunday'
CWeekday
D'Holiday'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around day names or output strings.
Using incorrect day names or output text.