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

223
Vistas
IF ELSE condition is always going to the ELSE

Here is my POST request:

router.post('/', (req, res) => {
    let body = { ...req.body };
    let odd = new Array();
    let even = new Array();
    let is_success = true;

    body.array.forEach((element) => {
        if (Number.isInteger(element)) {
            if (element % 2 == 0) {
                even.push(element);
            } else {
                odd.push(element);
            }
        } else {
            is_success = false;
            return;
        }
    });
    if (is_success) {
        res.status(200).json({
            is_success: `${is_success}`,
            user_id: 'john_doe_17091999',
            odd: `${odd}`,
            even: `${even}`,
        });
    } else
        res.status(200).json({
            is_success: `${is_success}`,
            user_id: 'john_doe_17091999',
        });
});

The condition is_success is not becoming true and that takes it to the else condition always. What I am trying to do is take the array and return odd and even arrays if all input elements are Integers:

{
"array": ["1", "2", "3"] 
}

Response:

{
"successful": true,
"odd": [1, 3],
"even": [2],
}

What it is always responding with:

{
    "is_success": "false",
    "user_id": "john_doe_17091999"
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your array contains strings. You need to convert them to numbers before you can run Number.isInteger() on them, otherwise it will always return 'false'. You can do that by putting a '+' before the element. See below.

Number.isInteger(+element)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You must cast every element to Number before using the Number.isInteger otherwise you'll get false.

Example: Number.isInteger("2") will return false Number.isInteger(Number("2")) will return true

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