简体   繁体   English

仅使用if / else语句检查JavaScript中的多个条件

[英]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. 如果有人可以仅在if / else语句中解释执行此操作的最佳方法,我将不胜感激。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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