I have created this code to find the arrays that have all their positive integers, but I don't understand why it only returns the first one it finds, and it does not accumulate the arrays in the general array.
const input = [[1, 10, -100], [2, 20, 200], [3, 30, 300]];
function positiveRowsOnly(array) {
let num = 0;
return array.filter(el=>{
for(let i=0;i<=el.length;i++){
if(el[i]>0){
num++;
}
}
if(el.length==num){
return el;
}
num = 0;
})
}
console.log(positiveRowsOnly(input));
The filter method creates a new array with the elements that have met the condition, as I have in this other simple code, which returns all the even numbers, it accumulates the result of the return within the array.
const input = [10, 15, 20, 25, 30, 35];
function onlyEven(array) {
return array.filter(num =>{
if(num%2==0){
return num;
}
})
}
onlyEven(input);