0

I'm trying to display a message if only the conditional statements meet a certain requirement but with what I'm getting it fails me. Currently, the script is like this

 if ((err.status === 401 && !request.url.includes("users") || !request.url.includes("friends")) ) {
      console.log('hello')

    }

What I want to achieve is when the status is 401 and the URL doesn't include either users or friends the console should log hello but it fails to work

when I change the code to this

   if ((err.status === 401 && !request.url.includes("users")) ) {
          console.log('hello')

        }

or this

 if ((err.status === 401 && !request.url.includes("friends")) ) {
          console.log('hello')

        }

it works, but I want it to check for both conditions

3
  • Just change || to && if you need to check that url not included 'users' AND not included 'friends' Commented Aug 20, 2020 at 9:29
  • if(err.status==401 && !(request.url.includes("users") || request.url.includes("friends"))) Commented Aug 20, 2020 at 9:37
  • a AND (not (b OR c)) === a AND (not b) AND (not c) en.wikipedia.org/wiki/De_Morgan%27s_laws Commented Aug 20, 2020 at 9:44

4 Answers 4

2

Just swap the || for a &&

if (err.status === 401 && !request.url.includes("users") && !request.url.includes("friends")) {
    console.log('hello')
}

If you think it through in your head it would sound like the following.

  1. The status has to be 401
  2. AND the url should NOT include users
  3. AND the url should NOT include friends

If you want to code it like you wrote it:

  1. The status has to be 401
  2. AND The url should NOT include users OR friends

You could still do that, but just a little different:

if (err.status === 401 && !(request.url.includes("users") || request.url.includes("friends"))) {
    console.log('hello')
}

Both these codes mean exactly the same, just write it how you prefer it more. But always write it like it flows in your head. Just type out what you think :)

Sign up to request clarification or add additional context in comments.

Comments

1

For string validations I suggest you to use Regular Expressions (It can allow more complex validations)

if(error.status === 401 && !/users|friends/.test(request.url)){
  console.log("hello")
}

Comments

0

The way that is parallel to the wording:

if(err.status==401 && !(request.url.includes("users") || request.url.includes("friends")))

Visually:

if
(
  err.status==401
  &&
  !
    (
      request.url.includes("users")
      ||
      request.url.includes("friends")
    )
)

Comments

0

your code is almost ok, just some issue with brackets ().

the format is :

if( cond1 && !(  cond2 || cond3  ) ) {
 ----- your logic ------
}

so your example should be like :

if ( err.status === 401 && !( request.url.includes("users") || request.url.includes("friends") ) )  {
            console.log('hello')
}

1 Comment

yes, you are right. I am going to change the code. thanks for pointing out the mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.