简体   繁体   中英

Checking multiple conditions in JavaScript with only if/else statements

This is a snippet of my code thus far:

    if ((num1 == 3 || num1 == 5  || num1 == 7) && (num2 == 2) && (num3 >= 5 && num3 <= 100) && (num4 < 9 && num4 > 20)) {
      return "correct";
    }else{
      return "incorrect";
     }
   };

If I separate each of these conditions they test fine individually, but chained like this they don't. I would really appreciate if someone could explain the best way to do this with only if/else statements. These are the test results:

✓ should be defined
✓ should return 'correct' for the correct example
✓ should return 'correct' for all correct first numbers
1) should return 'incorrect' for the incorrect first number example
2) should return 'incorrect' for incorrect second number
✓ should return 'correct' for the boundary conditions of the third number
3) should return 'incorrect' for invalid third numbers
4) should return 'incorrect' for the incorrect fourth number example
5) should return 'incorrect' for the boundary conditions of the fourth number

Again, any help would be appreciated, I really want to understand the best approach the this, and exactly whats going on. Thanks.

the

...&& (num4 < 9 && num4 > 20)

will be false always,

maybe you mean:

...&& (num4 < 9 || num4 > 20)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM