https://www.codewars.com/kata/5715eaedb436cf5606000381
You get an array of numbers, return the sum of all of the positives ones.
[1,-4,7,12]
=> 1 + 7 + 12 = 20
Note: if there is nothing to sum, the sum is default to 0
.
function positiveSum(arr) {
return answer = arr.reduce((arr, cur) => {
if (cur < 0) cur = 0;
return arr + cur;
}, 0);
}
if문을 3항 연산자로 구현
function positiveSum(arr) {
return arr.reduce((a,b)=> a + (b > 0 ? b : 0),0);
}