Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Condition on 4 numbers

I have two variables which must display an integer.

The two variables must be in 2 ranges 30...40 and 60...80

For example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 :

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);
 }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading