簡體   English   中英

僅使用if / else語句檢查JavaScript中的多個條件

[英]Checking multiple conditions in JavaScript with only if/else statements

到目前為止,這是我的代碼的一部分:

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

如果我將這些條件中的每一個分開,它們將分別進行良好的測試,但是像這樣進行鏈接時,它們就不會。 如果有人可以僅在if / else語句中解釋執行此操作的最佳方法,我將不勝感激。 這些是測試結果:

✓ 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

再次,任何幫助將不勝感激,我真的想了解最好的方法,以及到底發生了什么。 謝謝。

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

永遠都是假的

也許你的意思是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM