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

112
Visualizações
Return only the values if not inside the array in for loop

This array contains 1,5,

let numberArray = [1,5];

I'm trying to print numbers from 1 to 280 except for the values inside the numberArray,

 for (let i = 1; i < 281; i++) {
    numberArray.forEach((number) => {
      if (number != i) {
        console.log(i);
      }
    });
  }

I'm expecting the result to be 2,3,4,6,7...280 without printing 1 and 5 in this case. But I get like this,

enter image description here

It prints 2 times each number except 1 and 5. I want to completely omit the values inside the numberArray and print the other values. Really appreciate it if somebody could point me to the bug. Thanks.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It isn't a bug, your code is doing exactly what you are telling it too. You have your for loop which happens 280 times and then you have a forEach loop inside that which is happening twice every time the loop goes around. So the Foreach loop is actually happening. 558 times.

You can just use the .includes method to check i doesn't exist within the numberArray.

 for (let i = 1; i < 281; i++) {
    if(!numberArray.includes(i)){
       console.log(i);
    }
  }
about 4 years ago · Santiago Trujillo 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