Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Count of positives / sum of negatives

// 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.

function countPositivesSumNegatives(input) {
  let totalCount = [0, 0]
    if(input === null || input.length <= 0) totalCount = []
    else{
      input.forEach(number => {
          if(number > 0) totalCount[0] += 1
          else totalCount[1] += number
      })
    }
    return totalCount;
}

// With love @kouqhar
Source by www.codewars.com #
 
PREVIOUS NEXT
Tagged: #Count #positives #sum #negatives
ADD COMMENT
Topic
Name
3+7 =