2015-05-15 79 views
-1

这是我的代码片段迄今:检查在JavaScript中多个条件,只有if/else语句

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 

同样,任何帮助,将不胜感激,我真的想了解的最佳途径了这一点,并准确怎么回事。谢谢。

+0

我们不知道发生了什么。你可以澄清一下'num1','num2'等什么? – Downgoat

+0

什么是问题陈述?看起来像你正在进行的在线测试。请链接它。 – Bergi

+0

对不起,我是新手。我会尽我所能编辑。这是一个检查代码的四个数字的函数。 num1应该等于3,5或7,num2应该等于2,num3应该在5到100之间,num4应该小于9并且大于20.当每个条件被测试时,它将进行测试,但是当加入我有他们没有的方式。 –

回答

4

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

将始终为假,

也许你的意思是:

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

非常感谢您的回复,它是正确的!我不知道我是如何忽略这一点的,但我正在学习代码每隔一段时间就会对你有所帮助......再次感谢! –