Skip to content

Latest commit

 

History

History
22 lines (19 loc) · 305 Bytes

7 kyu - Product of Array Items.md

File metadata and controls

22 lines (19 loc) · 305 Bytes

Task

Calculate the product of all elements in an array.

If the array is nil or is empty, the function should return nil.

My solution

def product(arr)
  if arr.nil? 
    return nil
  else 
    arr.inject(:*)
  end
end

Better solution

def product(arr)
  arr&.reduce(:*)
end