Calculator • Exercise 4/4

Product with Reduce

00:00
0
20 points

Instructions

Create a function product that takes an array of numbers and returns their product (multiplication).

Use the reduce method (also known as inject).

product([1, 2, 3, 4])  # => 24 (1 * 2 * 3 * 4)
product([5, 2])        # => 10

reduce takes an initial value and a block that combines elements:
ruby
[1, 2, 3].reduce(0) { |acc, n| acc + n } # => 6

Your Code

Results

Click "Run Tests" to see results