Home > Enterprise >  return of positive array within an array
return of positive array within an array

Time:10-20

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);

CodePudding user response:

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));
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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));
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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));
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

the problem is where you set num=0.

when you return, you don't set num=0.

try to set num=0 before you returnas:

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){
     num = 0; // here!
     return el;
   }
   
   num = 0; 

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