0
0
Rubyprogramming~10 mins

Why Ruby has multiple control flow styles - Test Your Understanding

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

Complete the code to print numbers from 1 to 5 using a simple loop.

Ruby
for i in 1..[1]
  puts i
end
Drag options to blanks, or click blank then click option'
A7
B3
C10
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using a range that ends too high or too low.
2fill in blank
medium

Complete the code to use an 'if' statement to check if a number is even.

Ruby
number = 4
if number [1] 2 == 0
  puts "Even"
end
Drag options to blanks, or click blank then click option'
A%
B+
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of modulo.
3fill in blank
hard

Fix the error in the 'case' statement to match the variable 'grade'.

Ruby
grade = 'B'
case [1]
when 'A'
  puts 'Excellent'
when 'B'
  puts 'Good'
else
  puts 'Try again'
end
Drag options to blanks, or click blank then click option'
Aresult
Bscore
Cgrade
Dmark
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that does not exist or is unrelated.
4fill in blank
hard

Fill both blanks to create a hash with word lengths for words longer than 3 characters.

Ruby
words = ['cat', 'house', 'tree', 'a']
lengths = {}
for word in words
  if word[1] [2] 3
    lengths[word] = word[1]
  end
end
Drag options to blanks, or click blank then click option'
A.length
B>
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect comparison operators or forgetting the length method.
5fill in blank
hard

Fill all three blanks to create a hash of uppercase words and their lengths for words longer than 2 characters.

Ruby
words = ['dog', 'apple', 'it']
result = {}
for word in words
  if word.length [3] 2
    result[[1]] = [2]
  end
end
Drag options to blanks, or click blank then click option'
Aword.upcase
Bword.length
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up keys and values or using wrong comparison operators.