Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print a greeting message.
Ruby
puts [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string.
Using print instead of puts.
✗ Incorrect
In Ruby, puts prints the string to the screen. The string must be in quotes.
2fill in blank
mediumComplete the code to define a method that returns true if a number is even.
Ruby
def even_number?(num) num.[1] end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
odd? instead of even?.Trying to use non-existent methods like
is_even.✗ Incorrect
The Ruby method even? returns true if the number is even.
3fill in blank
hardFix the error in the test code to check if 5 is odd using RSpec.
Ruby
RSpec.describe 'Number' do it 'checks if 5 is odd' do expect(5.[1]).to be true end end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
even? which returns false for 5.Using non-existent methods like
is_odd.✗ Incorrect
In RSpec, to check if 5 is odd, use the Ruby method odd?.
4fill in blank
hardFill both blanks to create a hash of word lengths for words longer than 3 letters.
Ruby
lengths = {}
for word in words
if word.[2] 3
lengths[word] = word.[1]
end
end Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
length? which is not a Ruby method.Using incorrect comparison operators.
✗ Incorrect
Use word.size to get the length of the word and compare it with 3 using >.
5fill in blank
hardFill all three blanks to create a hash of uppercase words and their lengths for words longer than 4 letters.
Ruby
result = {}
for word in words
if word.length [3] 4
result[[1]] = [2]
end
end Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
word.downcase instead of uppercase.Using wrong comparison operators.
✗ Incorrect
Use word.upcase as the key, word.length as the value, and check if length is greater than 4 with >.