Challenge - 5 Problems
Ruby Find/Detect Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Find the first even number in an array
What is the output of this Ruby code that finds the first even number in the array?
Ruby
numbers = [1, 3, 5, 8, 10] result = numbers.find { |n| n.even? } puts result
Attempts:
2 left
💡 Hint
Remember,
find returns the first element matching the condition.✗ Incorrect
The method
find returns the first element for which the block returns true. Here, 8 is the first even number.❓ Predict Output
intermediate2:00remaining
Detect the first string longer than 4 characters
What does this Ruby code print when detecting the first string longer than 4 characters?
Ruby
words = ['cat', 'dog', 'horse', 'elephant'] result = words.detect { |w| w.length > 4 } puts result
Attempts:
2 left
💡 Hint
Check the length of each word in order.
✗ Incorrect
The first word longer than 4 characters is 'horse'.
❓ Predict Output
advanced2:00remaining
Find first hash with value greater than 10
What is the output of this Ruby code that finds the first hash with a value greater than 10?
Ruby
items = [{a: 5}, {b: 15}, {c: 8}]
result = items.find { |h| h.values.first > 10 }
puts resultAttempts:
2 left
💡 Hint
Look at the values inside each hash.
✗ Incorrect
The first hash with a value greater than 10 is {:b=>15}.
❓ Predict Output
advanced2:00remaining
Detect first number divisible by 7
What does this Ruby code output when detecting the first number divisible by 7?
Ruby
nums = [10, 14, 21, 28] result = nums.detect { |n| n % 7 == 0 } puts result
Attempts:
2 left
💡 Hint
Check each number in order for divisibility by 7.
✗ Incorrect
14 is the first number divisible by 7 in the array.
🧠 Conceptual
expert2:00remaining
Understanding find vs detect in Ruby
Which statement is true about the Ruby methods
find and detect?Attempts:
2 left
💡 Hint
Check Ruby documentation for these methods.
✗ Incorrect
In Ruby,
find and detect are exactly the same method with two names.