Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda