Home > front end >  Multiple and different conditions in Javascript
Multiple and different conditions in Javascript

Time:12-31

I have a problem with logic. If the number is higher than 0 and less than 10 AND the number is 11 AND the number is 22 do something. (1, 2, 3, 4, 5, 6, 7, 8, 9, 11 or 22).

And ELSE IF the number is higher than 10 BUT is NOT 11 or is NOT 22, do something else. (10, 12, 13 ....20, 21, 23, 24...)

I got to do only with 11, but I have no clue how to insert another condition with 22.

if (n >= 0 && n < 10) || (n == 11 && n == 22) {
        do something

    } else if (n >= 10 && n != 11) && (n != 22) {
        do something else
    }

Please, don't downvote. English is not my first language.

CodePudding user response:

Try (n >= 1 && n <= 9) || n == 11 || n == 22 which will check if the number is between 1 and 9 inclusive (1 to 9) OR it's 11 OR it's 22.

Then, in your else if, just check for >= 10 - numbers less than 1 e.g. 0, won't trigger the else either.

if ((n >=1 0 && n <= 9) || n == 11 || n == 22) {
    do something
} else if (n >= 10) {
    do something else
}

Use >= and <= to make it clearer what values you're looking for otherwise it takes a little extra mental load to realise that n > 0 && n < 10 really means values 1 to 9.

Also, it's JavaScript and numbers can either be integral or floating point, so greater than zero could be 0.1 etc.

CodePudding user response:

In your if statement, you say:

n is larger or equal to 0 AND n is smaller than 10 OR n is equal to 11 AND n is equal to 22

Which means, the if block will run for numbers that are between 0 and 9 (inclusive) OR for the numbers 11 AND 22. So the number can either be between 0 and 9 or it can be BOTH 11 and 22, which is impossible.

My take on the if statement:

if (n >= 1 && n <= 9 || n == 11 || n == 22) {
    do something;
}

That way, you don't get confused with the inclusive and exclusive numbering and make sure that either 11 or 22 is allowed.

The second block will run for numbers that are greater or equal to 10 AND not 11 AND not 22. Which is just fine.

else if (n >= 10 && n != 11 && n != 22) {
    do something else;
}

In this answer, I assume you don't want to entirely skip the 10. If you do, then in the second statement, you would check for n > 10.

Logically, this is sound. However, I don't think that you need to check for 11 and 22 in the second one, as the if block will run first and if the number is 11 or 22, the first if block will run. So the second one doesn't need to account for them.

CodePudding user response:

Based on your example of (1, 2, 3, 4, 5, 6, 7, 8, 9, 11 or 22) it sounds like this is what you're going for?

if ((n > 0 && n < 10) || n == 11 || n == 22) {
  // do something
} else if (n >= 10) {
  // do something else
}

Edit: Added else if (n >= 10) that I overlooked

CodePudding user response:

Just take the comparison and because of the higher precedence of logical AND && over logical OR ||, you need no parenthesis.

if (n >= 0 && n < 10 || n === 11 || n === 22) {
    // 1, 2, 3, 4, 5, 6, 7, 8, 9, 11 or 22
    // do something
} else {
    // 10, 12, 13 ... 21, 23 ...
    // do something
}

CodePudding user response:

Your first condition will never succeed because you can't have a number higher than 0 and less than 10 AND this same number is equal to 11 AND also being equal to 22 (in both case, it'll be more than 10).

if ((n > 0 && n < 10) || n == 11 || n == 22) {
    // my number is 1, 2, 3, 4, 5, 6, 7, 8, 9, 11 or 22
} else {
    // my number is almost everything except the values from above
}

CodePudding user response:

Take a look at the documentation, don't confuse logicial AND (&&) and logical OR (||).

function check(n) {
  if((n > 0 && n < 10) || (n == 11 || n == 22)) {
    console.log(n, true);
  } else if(n >= 10) {
    console.log(n, false);
  }
}

check(1)  // true
check(11) // true
check(22) // true
check(0)
check(10) // false
check(12) // false
check(21) // false
check(23) // false

CodePudding user response:

You were almost there.

To test for 1, 2, 3, 4, 5, 6, 7, 8, 9, 11 or 22:

if (n >= 0 && n < 10) || (n == 11 && n == 22) {

Can minimally be changed to:

if ((n > 0 && n < 10) || (n == 11 || n == 22)) {

The three important changes are:

  • balance parentheses to make a proper if( ) construct.
  • >= 0 becomes > 0 to achieve the higher than zero requirement.
  • The last && becomes ||.

This also changes the order of operations such that we don't actually need any extra parentheses at all in this case. This also happens to be correct, and is simpler:

if (n > 0 && n < 10 || n == 11 || n == 22) {

...because && has higher precedence than || and the comparators (>, < and ==) have higher precedence than the conditionals (&& and ||).

At this point you don't need an else if condition. Just else will suffice. 11 and 22 are handled by the affirmative case of the if and therefore n will be neither of those things in the else case.

Final product:

if (n > 0 && n < 10 || n == 11 || n == 22) {
  // 1, 2, 3, 4, 5, 6, 7, 8, 9, 11 or 22
} else {
  // Something else.  :)
}
  • Related