Code Lab

Sum of positives

EasyArrays3 min

Write a function sumPositive(numbers) that adds up only the positive numbers of an array and returns the sum. If there is nothing to add, return 0.

Examples

sumPositive([1, -4, 7, 12])

Returns 20

1 + 7 + 12, and -4 is skipped.
sumPositive([])

Returns 0

sumPositive([-1, -2])

Returns 0

Source

Loading the editor...
  1. InputsumPositive([ 1, -4, 7, 12 ])
    Expected20
  2. InputsumPositive([])
    Expected0
  3. InputsumPositive([ -1, -2 ])
    Expected0

Hidden tests run on Submit.