If logic with bool type and Object type

Asked

Viewed 51 times

-1

Hello, I’m cracking my head on this logic and I can’t seem to solve it;

I have that code:

for(let i = 0; i < mensagemDeErroDoInput.length; i++) 
{
    if(statusInput[mensagemDeErroDoInput[i].nome] === true)
    {
        return mensagemDeErroDoInput[i].mensagem;
    }
}

An IF inside a FOR, very simple!

Think of it this way: 1-I have two types of parameters, one parameter returns me bool and the other returns me an Object;

return parameter bool, return me this: true;

return parameter Object, return me this: {Valid: false};

These parameters are my error message references, that is, if my bool return parameter is true, then I return an error message.

And if my Object return parameter is different from null, then I return an error message.

However, all my bool return parameters are automatically already different from null. So I’m not getting that validation inside my.

Could someone please help me??

Thank you in advance...

  • What do you call "bool parameter" and "Object parameter"? I found these nomenclatures confusing, and this makes it a little difficult to understand the question.

  • Oh yes, I will edit my question, thus improving the reading. I appreciate the tip

  • Those "return parameters" are the mensagemDeErroDoInput[i].nome? If they are not, what (is) are the respective variables/expressions that store them?

  • Yes, that’s right! Then. One is Boolean, and the other I’m not sure, because I’m using typescript + angular and I can’t find the type of variable. But the return of this 'Object' is exactly as I showed above in the question

3 answers

1

I think what you want is this:

for (let i = 0; i < mensagemDeErroDoInput.length; i++) {
    var erro = mensagemDeErroDoInput[i];
    var status = statusInput[erro.nome];
    if (typeof status === "boolean" ? status === true : status !== null) {
        return erro.mensagem;
    }
}
  • Not quite! I just edited the question stating the type of return I get from these parameters.. Thanks

1

  • I had beautiful this page before writing this question and it’s not what I need. : / ta complicated this kkk. But thank you

1


Test the desired type and value as per:

for(let i = 0; i < mensagemDeErroDoInput.length; i++) 
{
    var statusInputCorrente = statusInput[mensagemDeErroDoInput[i].nome];
    if((typeof statusInputCorrente === "object" && statusInputCorrente != null) || (typeof statusInputCorrente === "boolean" && statusInputCorrente === true))
    {
        return mensagemDeErroDoInput[i].mensagem;
    }
}
  • Perfect Kra, it worked 100%! Thank you very much

  • Grateful to be able to help you @Guilhermenunes.

Browser other questions tagged

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