Count positives, sum negatives
Thu Feb 23 2023 10:38:18 GMT+0000 (Coordinated Universal Time)
Saved by
@AlanaBF
#javascript
function countPositivesSumNegatives(input) {
if (!input || !input.length) return []
let positives = 0
let negSum = 0
for(const num of input){
if(num > 0)positives++
else negSum+=num
}
return [positives, negSum]
}
//OR
function countPositivesSumNegatives(input) {
if (!input || !input.length)
return []
let countPositive = 0
let sumNegative = 0
for (let i=0; i<input.length; i++)
if(input[i] > 0) {
countPositive++;
}
else if(input[i] < 0) {
sumNegative += input[i]};
return [countPositive, sumNegative]
}
content_copyCOPY
Comments