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

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

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 Denunciar

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