Answers for "You are getting a `numbers` array. Return the sum of **negative** numbers only. //condition to check for negative"

0

You are getting a `numbers` array. Return the sum of **negative** numbers only. //condition to check for negative

function SummPositive( numbers ) {
  var negatives = [];
  var sum = 0;

  for(var i = 0; i < numbers.length; i++) {
    if(numbers[i] < 0) {
      negatives.push(numbers[i]);
    }else{
      sum += numbers[i];
    }
  }

  console.log(negatives);

  return sum;
}

var sum_result = SummPositive( [ 1, 2, 3, 4, 5, -2, 23, -1, -13, 10,-52 ] );

console.log(sum_result);
Posted by: Guest on March-20-2021

Code answers related to "You are getting a `numbers` array. Return the sum of **negative** numbers only. //condition to check for negative"

Browse Popular Code Answers by Language