任何人都可以帮助完成以下行的完全相反。我试过插入不!但我认为我必须把它放在错误的地方。
if (email.indexOf("@") < 1 || email.lastIndexOf(".") < email.indexOf("@") + 2 || email.lastIndexOf(".") + 2 >= email.length) {
alert ("I need this to show when the exact opposite of the if statement occurs")
}
非常感谢任何帮助
答案 0 :(得分:5)
让我们使用De Morgan's Law:
if(email.indexOf("@") >= 1 && email.lastIndexOf(".") >= email.indexOf("@") + 2 &&
email.lastIndexOf(".") + 2 < email.length)
{
alert ("I need this to show when the exact opposite of the if statement occurs")
};
只是对所有文字进行否定,并将“或”更改为“和”
答案 1 :(得分:1)
试试这个:
if (!(conditions))
if (!(email.indexOf("@") < 1 || email.lastIndexOf(".") < email.indexOf("@") + 2 || email.lastIndexOf(".") + 2 >= email.length)) {
alert ("I need this to show when the exact opposite of the if statement occurs")
}
答案 2 :(得分:0)
简单的答案是......
if (!(email.indexOf("@") < 1 || email.lastIndexOf(".") < email.indexOf("@") + 2 || email.lastIndexOf(".") + 2 >= email.length)) {
alert ("I need this to show when the exact opposite of the if statement occurs")
}
你也可以使用DeMorgans Laws否定整个陈述,而不是包裹在!( )
中。
if (email.indexOf("@" >= 1 && email.lastIndexOf(".") >= email.indexOf("@") + 2 && email.lastIndexOf(".") + 2 < email.length) {
alert ("I need this to show when the exact opposite of the if statement occurs")
}
答案 3 :(得分:0)
您可以通过将其括在括号(...)
中并在其前面添加!
来否定任何条件的结果。
所以,要天真地否定这种情况,请说:
if (!(email.indexOf("@") < 1 || email.lastIndexOf(".") < email.indexOf("@") + 2 || email.lastIndexOf(".") + 2 >= email.length)) {
...
然而,这通常导致无法读取的代码。要重构条件,请使用DeMorgan's Laws。
!(email.indexOf("@") < 1 || email.lastIndexOf(".") < email.indexOf("@") + 2 || email.lastIndexOf(".") + 2 >= email.length)
相当于:
(email.indexOf("@") >= 1 && email.lastIndexOf(".") >= email.indexOf("@") + 2 && email.lastIndexOf(".") + 2 < email.length)
为了进行这种转换,我们将每个||
更改为&&
并取消了表达式中的每个布尔运算符。