简体   繁体   English

Javascript 中的条件不起作用

[英]Condition in Javascript doesn't work

I'm about to get mad because of those conditions in JavaScript.由于 JavaScript 中的这些条件,我快要生气了。 Why this one doesn't work?为什么这个不起作用?

if (isNaN(first) || isNaN(second) && (act !== '-' || act !== '+'))

It should check is first is number, second is number and act is minus or plus.它应该检查第一个是数字,第二个是数字,行为是减号还是加号。 It's all right with numbers but it doesn't check act correct.数字没问题,但它不检查行为是否正确。 What's wrong?怎么了?

Eg first is 1, act is a, second is 2. It won't work.例如,第一个是 1,行为是一个,第二个是 2。这行不通。

PS聚苯乙烯

I've some knowledge in C++ and Java.我对 C++ 和 Java 有一些了解。 I've never had any problems with it.我从来没有遇到过任何问题。 But since the time I've started to learn Javascript, I get so many mistakes like this.但是自从我开始学习 Javascript 以来,我犯了很多这样的错误。

Is there different system of such expressions?这种表达方式有不同的系统吗?

It should check is first is number, second is number and act is minus or plus.它应该检查第一个是数字,第二个是数字,行为是减号还是加号。 It's all right with numbers but it doesn't check act correct.数字没问题,但它不检查行为是否正确。

Thus for the requirement stated above the correct statement would be因此,对于上述要求,正确的陈述是

if ( !isNaN(first) && !isNaN(second) && (act == '-' || act == '+'))

If you want to invert the condition, that would be如果你想反转条件,那就是

if ( !(!isNaN(first) && !isNaN(second) && (act == '-' || act == '+')))

which is equivalent to相当于

if ( isNaN(first) || isNaN(second) || (act != '-' && act != '+'))

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

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