Suma de solo números positivos

PHOTO EMBED

Sat May 20 2023 14:49:00 GMT+0000 (Coordinated Universal Time)

Saved by @meridaK #javascript #array #operaciónmate #for

function positiveSum(arr) {
  var total = 0;    
  for (i = 0; i < arr.length; i++) {   
    if (arr[i] > 0) {                   // if arr[i] es mayor a 0
      total += arr[i];                  // dar el total de la suma de esos arr[i]
    }
  }
  return total;                         // return total
}
content_copyCOPY

Devuelve la suma de todos los positivos. Ejemplo [1,-4,7,12] => 1 + 7 + 12 = 20

https://www.codewars.com/kata/5715eaedb436cf5606000381/solutions/javascript