0
0
Rubyprogramming~10 mins

Case with ranges and patterns 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 match the variable 'score' with the correct range in the case statement.

Ruby
score = 75
case score
when [1]
  puts "Passed"
else
  puts "Failed"
end
Drag options to blanks, or click blank then click option'
A0..49
B51..74
C100..150
D50..100
Attempts:
3 left
💡 Hint
Common Mistakes
Using a range that does not include 75, like 0..49.
2fill in blank
medium

Complete the code to print the correct message for the variable 'age' using a case statement with ranges.

Ruby
age = 20
case age
when [1]
  puts "Teenager"
when 20..29
  puts "Twenties"
else
  puts "Other age"
end
Drag options to blanks, or click blank then click option'
A0..12
B13..19
C10..15
D30..39
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a range that does not cover all teenage years.
3fill in blank
hard

Fix the error in the case statement to correctly match the variable 'grade' with letter grades.

Ruby
grade = 'B'
case grade
when [1]
  puts "Excellent"
when 'B'
  puts "Good"
else
  puts "Needs Improvement"
end
Drag options to blanks, or click blank then click option'
A'A'
BA
C"A"
D:A
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted letters which cause errors.
4fill in blank
hard

Fill both blanks to create a case statement that matches 'temperature' with descriptive ranges.

Ruby
temperature = 15
case temperature
when [1]
  puts "Cold"
when [2]
  puts "Warm"
else
  puts "Hot"
end
Drag options to blanks, or click blank then click option'
A0..10
B11..20
C21..30
D31..40
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the ranges or using overlapping ranges.
5fill in blank
hard

Fill both blanks to create a case statement that matches 'day' with weekdays and weekends.

Ruby
day = 'Saturday'
case day
when [1]
  puts "Weekday"
when [2]
  puts "Weekend"
else
  puts "Invalid day"
end
Drag options to blanks, or click blank then click option'
A/Monday|Tuesday|Wednesday|Thursday|Friday/
B/Saturday|Sunday/
C"Saturday"
D"Monday"
Attempts:
3 left
💡 Hint
Common Mistakes
Using single strings instead of regex patterns for multiple days.