Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda