Condition on 4 numbers
Contents
hide
Problem Description:
I have two variables which must display an integer.
The two variables must be in 2 ranges 30...40
and 60...80
For example:
If the first number displays 35
and the second number 65
. The condition is true
.
Another example:
If the first number displays 35
and the second 110
, the condition is false
.
My problem is that for the first number, I initialize the value to 35
and the second to 65
and I obtain a false
instead of true
.
let nb1 = 35;
let nb2 = 65;
if( (nb1 >= 30 && nb1 <= 40 && nb1 >= 60 && nb1 <= 80) && (nb2 >= 30
&& nb2 <= 40 && nb2 >= 60 && nb2 <= 80) ){
console.log(true);
} else {
console.log(false);
}
What is wrong with my condition?
Thank you
Solution – 1
The number can’t be in both ranges simultaneously, you need to use an or
condition:
let nb1 = 35;
let nb2 = 65;
if(((nb1 >= 30 && nb1 <= 40) || (nb1 >= 60 && nb1 <= 80)) && ((nb2 >= 30
&& nb2 <= 40) || (nb2 >= 60 && nb2 <= 80))){
//condition(s) are met
console.log(true);
} else {
console.log(false);
}