# Finding the positive numbers count in an ECMAScipt array

All I am trying to achieve is better use of built-in ES functions.

var arr = [0, 1, 2, 3, -1, -2, -3];

function countPositiv(p, c, i, a)
{
if(c > 0){
return p+1;
} else {
return p;
}
}

var positiv = arr.reduce(countPositiv, 0);

console.log(positiv);

• Welcome to Code Review! I hope you get some helpful reviews. – SirPython Jan 25 '16 at 23:34

## 2 Answers

I don't think there is much to review, it looks ok. I would just do it inline, and there is no need to list arguments you are not using:

var positiv = arr.reduce(function(acc, x) {
return x > 0 ? acc + 1 : acc;
}, 0);


This can easily be done using filter and length. filter creates an array comprising of values where the callback returned true. In this case, when v > 0. And since filter returns an array, we have access to length.

let positiveNumbersCount = arr.filter(v => v > 0).length;