Skip to content

Latest commit

 

History

History
25 lines (21 loc) · 414 Bytes

8 kyu - Sum of positive.md

File metadata and controls

25 lines (21 loc) · 414 Bytes

Task

You get an array of numbers, return the sum of all of the positives ones.

Example [1,-4,7,12] => 1 + 7 + 12 = 20

Note: if there is nothing to sum, the sum is default to 0.

Solution

def positive_sum(arr)
  arr.delete_if{ |x| x < 0 }
  if arr.empty?
    0
  else
    arr.inject(:+)
  end
end

Alternative solution

def positive_sum(arr)
  arr.select{|x| x > 0}.reduce(0, :+)
end