Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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);
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #You #Return #sum #numbers #check #negative
ADD COMMENT
Topic
Name
9+6 =