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

156
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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