Challenge - 5 Problems
Reject Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Ruby code using reject?
Consider the following Ruby code that uses
reject to filter an array. What will be printed?Ruby
numbers = [1, 2, 3, 4, 5] result = numbers.reject { |n| n.even? } puts result.inspect
Attempts:
2 left
💡 Hint
Remember, reject removes elements for which the block returns true.
✗ Incorrect
The
reject method removes elements where the block returns true. Here, it removes even numbers, so only odd numbers remain.❓ Predict Output
intermediate2:00remaining
What does this code output when using reject with a hash?
Given this Ruby code using
reject on a hash, what is the output?Ruby
scores = { alice: 90, bob: 75, carol: 85 }
filtered = scores.reject { |name, score| score < 80 }
puts filtered.inspectAttempts:
2 left
💡 Hint
Reject removes pairs where the block returns true.
✗ Incorrect
The block returns true for scores less than 80, so :bob is removed. The remaining pairs are alice and carol.
🔧 Debug
advanced2:00remaining
Why does this reject code raise an error?
This Ruby code raises an error. What is the cause?
Ruby
arr = [1, 2, 3] result = arr.reject do |x| x > 1 end puts result.inspect
Attempts:
2 left
💡 Hint
Check if the block syntax is correct and what reject expects.
✗ Incorrect
The code is valid. reject removes elements where block returns true (x > 1), so it removes 2 and 3, leaving [1].
❓ Predict Output
advanced2:00remaining
What is the output of reject with a complex condition?
What does this Ruby code print?
Ruby
words = ['apple', 'banana', 'cherry', 'date'] result = words.reject { |w| w.length <= 5 } puts result.inspect
Attempts:
2 left
💡 Hint
Reject removes words with length less than or equal to 5.
✗ Incorrect
Words with length <= 5 are 'apple' (5) and 'date' (4), so they are removed. Remaining are 'banana' and 'cherry'.
❓ Predict Output
expert3:00remaining
What is the output of this nested reject code?
Analyze this Ruby code with nested reject calls. What is printed?
Ruby
data = { a: [1, 2, 3], b: [4, 5, 6], c: [7, 8, 9] }
filtered = data.reject do |key, arr|
arr.reject { |x| x.even? }.empty?
end
puts filtered.inspectAttempts:
2 left
💡 Hint
Check what the inner reject returns and when the outer reject removes a key.
✗ Incorrect
Inner reject removes even numbers. If the resulting array is empty, outer reject removes that key. All arrays have odd numbers, so none become empty after inner reject. So no keys are removed, filtered equals original data.