Instructions
Create a function sum that takes an array of numbers and returns their sum.
Hint: Ruby arrays have a sum method!
Hints:
- Try using the .sum method on arrays
- Or use .reduce(:+) for a more manual approach
Your Code
def sum(numbers) numbers.sum end
RSpec.describe "sum" do
it "sums an array of numbers" do
expect(sum([1, 2, 3, 4])).to eq(10)
end
it "returns 0 for empty array" do
expect(sum([])).to eq(0)
end
it "handles negative numbers" do
expect(sum([-1, 1, -2, 2])).to eq(0)
end
end
Results
Click "Run Tests" to see results