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