0
0
Rubyprogramming~10 mins

Find/detect for first match 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 find the first even number in the array.

Ruby
numbers = [1, 3, 4, 6, 7]
first_even = numbers.[1] { |n| n.even? }
puts first_even
Drag options to blanks, or click blank then click option'
Aeach
Bselect
Cmap
Dfind
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'select' returns all matching elements, not just the first.
Using 'map' transforms all elements, not find one.
2fill in blank
medium

Complete the code to detect the first word longer than 5 characters.

Ruby
words = ['apple', 'banana', 'pear', 'grape']
long_word = words.[1] { |word| word.length > 5 }
puts long_word
Drag options to blanks, or click blank then click option'
Areject
Bselect
Cdetect
Deach
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'select' returns all matching elements, not just the first.
Using 'reject' returns elements that do not match.
3fill in blank
hard

Fix the error in the code to find the first number divisible by 3.

Ruby
nums = [2, 4, 6, 9, 12]
result = nums.[1] { |num| num % 3 == 0 }
puts result
Drag options to blanks, or click blank then click option'
Afind
Bmap
Cselect
Deach
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'select' returns an array, not a single element.
Using 'map' transforms elements instead of filtering.
4fill in blank
hard

Fill both blanks to create a hash of words and their lengths, but only for words longer than 4 characters.

Ruby
words = ['cat', 'elephant', 'dog', 'giraffe']
lengths = words.select { |word| word.length > 4 }.map { |word| [[1], [2]] }.to_h
Drag options to blanks, or click blank then click option'
Aword
Bword.length
Cword.size
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'len' which is not a Ruby method.
Using 'word.size' is valid but not the expected answer here.
5fill in blank
hard

Fill both blanks to find the first number greater than 10 and print it.

Ruby
numbers = [4, 7, 12, 15, 3]
first_big = numbers.[1] { |n| n [2] 10 }
puts first_big
Drag options to blanks, or click blank then click option'
Afind
B>
Dselect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'select' returns all matching numbers, not just the first.
Putting an extra method after puts causes errors.