HEAD
Given an array of integers. Return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers. 0 is neither positive nor negative. If the input is an empty array or is null, return an empty array. Example
For input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15], you should return [10, -65].
function countPositivesSumNegatives(input) {
if(input == null || input.length < 1){
return []
}
else{
let countOfPostives = input.filter( x => x > 0)
let sumOfNegatives = input.filter( x => x < 0).reduce((a,b) => a + b, 0)
return [countOfPostives.length, sumOfNegatives];
}
}
OR
function countPositivesSumNegatives(input) {
let arrayAnswer = input && input.length ? [input.filter(x => x > 0).length, input.filter( y => y < 0).reduce((item1, item2) => item1 + item2,0) ]: []
return arrayAnswer;
}
Try it yourself - CodeWars
Given an array of integers. Return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers. 0 is neither positive nor negative. If the input is an empty array or is null, return an empty array. Example
For input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15], you should return [10, -65].
function countPositivesSumNegatives(input) {
if(input == null || input.length < 1){
return []
}
else{
let countOfPostives = input.filter( x => x > 0)
let sumOfNegatives = input.filter( x => x < 0).reduce((a,b) => a + b, 0)
return [countOfPostives.length, sumOfNegatives];
}
}
OR
function countPositivesSumNegatives(input) {
let arrayAnswer = input && input.length ? [input.filter(x => x > 0).length, input.filter( y => y < 0).reduce((item1, item2) => item1 + item2,0) ]: []
return arrayAnswer;
}
Try it yourself - CodeWars