Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
return of positive array within an array

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);
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can actually simplify your code - just combine .filter with .every on the nested arrays to filter out those arrays containing negative numbers:

const input = [[1, 10, -100], [2, 20, 200], [3, 30, 300]];

function positiveRowsOnly(array) {
  
  return array.filter(subArr =>{    
    return subArr.every(el => el > 0);
  });
}
console.log(positiveRowsOnly(input));

about 4 years ago · Juan Pablo Isaza Report

0

You need to assign the num=0 inside the filter. Here is the working code.

const input = [[1, 10, -100], [2, 20, 200], [3, 30, 300]];

function positiveRowsOnly(array) {
  
  return array.filter(el=>{
   let num = 0;
   for(let i=0;i<=el.length;i++){
     
     if(el[i]>0){
       num++;
     }    
   }
    
   if(el.length==num){
     return el;
   }

  })
}
console.log(positiveRowsOnly(input));

about 4 years ago · Juan Pablo Isaza Report

0

This happens because you declared num outside your filter function, so it gets incremented everytime, and this condition:

if(el.length==num){
  return el;
}

Never works. To fix it, just move the let num = 0 declaretion inside the filter function, like this:

const input = [[1, 10, -100], [2, 20, 200], [3, 30, 300]];
    
function positiveRowsOnly(array) {  
  return array.filter(el=>{
    let num = 0;
        
    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));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!