Instructions
Create a function find_max that takes an array of numbers
and returns the largest number.
Hint: Arrays have a built-in max method!
Hints:
- Use .max method
- Or use .reduce with comparison
- Handle empty arrays (return nil)
Your Code
def find_max(numbers) numbers.max end
RSpec.describe "find_max" do
it "finds max in positive numbers" do
expect(find_max([1, 5, 3, 9, 2])).to eq(9)
end
it "finds max with negative numbers" do
expect(find_max([-5, -1, -10])).to eq(-1)
end
it "returns nil for empty array" do
expect(find_max([])).to be_nil
end
end
Results
Click "Run Tests" to see results