Instructions
Create a function double_each that takes an array of numbers
and returns a new array with each number doubled.
Use the map method for this transformation.
Hints:
- Use .map to transform each element
- map returns a new array
- Example: [1,2].map { |n| n * 2 } returns [2,4]
Your Code
def double_each(numbers)
numbers.map { |n| n * 2 }
end
RSpec.describe "double_each" do
it "doubles each number" do
expect(double_each([1, 2, 3])).to eq([2, 4, 6])
end
it "handles empty array" do
expect(double_each([])).to eq([])
end
it "handles negative numbers" do
expect(double_each([-1, -2])).to eq([-2, -4])
end
end
Results
Click "Run Tests" to see results