Instructions
Create a function select_evens that takes an array of numbers
and returns a new array containing only the even numbers.
Use the select method for filtering.
Hints:
- Use .select to filter elements
- select keeps elements where the block returns true
- Use n.even? or n % 2 == 0
Your Code
def select_evens(numbers)
numbers.select { |n| n.even? }
end
RSpec.describe "select_evens" do
it "selects even numbers" do
expect(select_evens([1, 2, 3, 4, 5, 6])).to eq([2, 4, 6])
end
it "returns empty for all odds" do
expect(select_evens([1, 3, 5])).to eq([])
end
it "handles empty array" do
expect(select_evens([])).to eq([])
end
end
Results
Click "Run Tests" to see results