Count of positives / sum of negatives

Lee·2022년 6월 21일
0

Algorithm

목록 보기
23/92
post-thumbnail

❓ Count of positives / sum of negatives

Q. 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].

✔ Solution

function countPositivesSumNegatives(input) {
  if (input == null || input.length < 1) {
    return [];
  }
  var sum = 0;
  var pos = [];

  for (var i = 0; i < input.length; i++) {
    if (input[i] > 0) {
      pos.push(input[i]);
    } else {
      sum += input[i];
    }
  }
  return [pos.length, sum];
}

//use .map( )
function countPositivesSumNegatives(input) {
  let sumOfPositive = 0;  
  let sumOfNegative = 0; 
  
  if(input == null || input.length < 1) {
    return [];
    } else {
      input.map(item => {
        if(item > 0) {
          sumOfPositive++;
        } else if(item < 0) {
          sumOfNegative += item;
        } else {
          return []
        }
      })
    }
  return [sumOfPositive, sumOfNegative]
}
profile
Lee

0개의 댓글