We are supposed to return the count of all the positive numbers given an array, and the addition of all the numbers given the same array. Could someone tell me what I am doing wrong please. I would really appreciate it. This is what I put as my code(JavaScript):
function countPositivesSumNegatives(input) {
let arr = [];
let count = 0;
let neg = 0;
for (let i = 0; i <= input.length; i ) {
if (input[i] > 0) {
count ;
} else if (input[i] < 0) {
neg = input[i];
}
return arr.push(count, neg);
}
}
CodePudding user response:
function countPositivesSumNegatives(input) {
let count = 0;
let neg = 0;
for (let i = 0; i <= input.length; i ) {
if (input[i] > 0) {
count ;
} else if (input[i] < 0) {
neg = input[i];
}
}
// Return outside the for loop
return [count, neg];
}
console.log(countPositivesSumNegatives([1,2,4,-1,0,-2,3,-4]))
CodePudding user response:
function countPositivesSumNegatives(input) {
let count = 0;
let neg = 0;
for (const number of input) {
if (number > 0) {
count ;
} else if (number < 0) {
neg = number;
}
}
return [count, neg];
}