How to know which parole is giving true?

Asked

Viewed 115 times

8

For example, I have the following if:

if(!condicao1 || !condicao2 || !condicao3){
  retorno erro com a condiçao que nao existe
}

When he gets into that if i wonder which of the parameters is missing to return an error.

  • To know which, they need to be separated under different conditions.

  • If it is more than one you want to return all or the first only?

  • It is important to mark the answer that helped you the most.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

  • Gives a console.log in each of them ;)

2 answers

12

It’s simple, if you want to know individually have to do individually:

if (!condicao1) {
    //Faça o que precisa aqui
}
if (!condicao2) {
    //Faça o que precisa aqui
}
if (!condicao3) {
    //Faça o que precisa aqui
}

Eventually you can do something general too:

if (!condicao1 || !condicao2 || !condicao3) {
    //faz algo geral aqui
    if (!condicao1) {
        //Faça o que precisa aqui
    }
    if (!condicao2) {
        //Faça o que precisa aqui
    }
    if (!condicao3) {
        //Faça o que precisa aqui
    }
}

I put in the Github for future reference.

  • +1 because it also works. :)

4

It seemed to me a conflict of title with the text of the question: missing = false? I took into account the title because it is more clear that you want to know which one is giving true.

If you want to know 1 of the parameters is true, can do so:

condicao1 = false;
condicao2 = true;
condicao3 = false;

if(!condicao1 || !condicao2 || condicao3){
    if(condicao1 || condicao2){
        if(condicao1){
            alert("condicao1 é true");
        }else{
            alert("condicao2 é true");
        }
    }else{
        alert("condicao3 é true");
    }
}

Or you can do it straight:

condicao1 = false;
condicao2 = true;
condicao3 = false;

if(condicao1 || condicao2){
    if(condicao1){
        alert("condicao1 é true");
    }else{
        alert("condicao2 é true");
    }
}else{
    alert("condicao3 é true");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.