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

150
Vistas
if statement meets multiple conditions but stops at first condition that matches

I am using an if statement and trying to make sure that it stops when it meets most of the conditions that it can. For example, if I have the following code:

  const determineLevel = () => {
    const wentToKindergarten = true;
    const wentToElementary = true;
    const wentToHighSchool = true;
    const wentToCollege = false;

    if (wentToKindergarten) {
      return 1;
    }

    if (wentToKindergarten && wentToElementary) {
      return 2;
    }

        if (wentToKindergarten && wentToElementary && wentToHighSchool) {
      return 3;
    }

 
     if (wentToKindergarten && wentToElementary && wentToHighSchool && wentToCollege) {
      return 4;
    }

    return;
  };

In this example, if a student went to kindergarten, elementary, and high school, but skipped out of college, how do I implement this? Right now, it just stops at the first condition it meets.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

  const determineLevel = () => {
    const wentToKindergarten = true;
    const wentToElementary = true;
    const wentToHighSchool = true;
    const wentToCollege = false;
    var returnme=0;


    if (wentToKindergarten) {
      returnme= 1;
    }

    if (wentToKindergarten && wentToElementary) {
      returnme= 2;
    }

        if (wentToKindergarten && wentToElementary && wentToHighSchool) {
      returnme= 3;
    }

 
     if (wentToKindergarten && wentToElementary && wentToHighSchool && wentToCollege) {
      returnme= 4;
    }

    return returnme;
  };

Edit: Or you might prefer to place the conditionals in reverse order so that hardest to satisfy is checked first. Whatever you prefer.

about 4 years ago · Juan Pablo Isaza Denunciar

0

The return statement finishes the function, a function will not execute past the first return statement that it finds. So a better way to do your code would be something like:

const determineLevel = () => {
    var returnNumber;
    const wentToKindergarten = true;
    const wentToElementary = true;
    const wentToHighSchool = true;
    const wentToCollege = false;

    if (wentToKindergarten) {
      returnNumber = 1;
    }

    if (wentToKindergarten && wentToElementary) {
      returnNumber = 2;
    }

        if (wentToKindergarten && wentToElementary && wentToHighSchool) {
      return = 3;
    }

 
     if (wentToKindergarten && wentToElementary && wentToHighSchool && wentToCollege) {
      returnNumber = 4;
    }

    if(returnNumber > 0)
    {
        return returnNumber;
    }
    return;
  };

This way you check through all scenarios then return whatever number you need at the end.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your code was not working for your logic because, you are returning a value inside each if condition check, which will cause the function to stop execution after the first condition becomes true.

You can change the if condition to else-if conditions.

Or you can have a variable to keep track off,

const determineLevel = () => {
const wentToKindergarten = true;
const wentToElementary = true;
const wentToHighSchool = true;
const wentToCollege = false;
let condition;
if (wentToKindergarten) {
  condition= 1;
}

if (wentToKindergarten && wentToElementary) {
  condition= 2;
}

if (wentToKindergarten && wentToElementary && wentToHighSchool) {
  condition= 3;
}


 if (wentToKindergarten && wentToElementary && wentToHighSchool && wentToCollege) {
  condition= 4;
}

 return condition;
};
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