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

203
Visualizações
Why the code provides the odd number from the array?

could you please help me understand why the following line result is [1,3]?

[1,3,6].filter( item => item % 2)

I was expecting to receive the even number from the array.

Many thanks!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Filter checks for a boolean, 3 % 2 is equal to 1, which evaluates to true.

You want item % 2 == 0

about 4 years ago · Juan Pablo Isaza Relatório

0

Even numbers are those numbers that are exactly divisible by 2.

The remainder operator % gives the remainder when used with a number. For example,

const number = 6;
const result = number % 4; // 2 

Hence, when % is used with 2, the number is even if the remainder is zero. Otherwise, the number is odd.

// program to check if the number is even or odd
// take input from the user
const number = prompt("Enter a number: ");

//check if the number is **even**
if(number % 2 == 0) {
    console.log("The number is even.");
}

// if the number is **odd**
else {
    console.log("The number is odd.");
}

ref : https://www.programiz.com/javascript/examples/even-odd

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