JavaScript代码可能存在什么问题?

huangapple go评论66阅读模式
英文:

What could be the problem with the javascript code?

问题

The code you provided seems to have an issue. It will always return 'TRUE' because of the logic in the if statement. If you want it to return 'TRUE' only when momo_no.substring(0, 3) is not equal to any of those values, you should use && instead of || in your conditions. Here's the corrected code:

var momo_no = '0759933091';
if (momo_no.substring(0, 3) != '075' && momo_no.substring(0, 3) != '070' && momo_no.substring(0, 3) != '074' && momo_no.substring(0, 3) != '077' && momo_no.substring(0, 3) != '078' && momo_no.substring(0, 3) != '076' && momo_no.substring(0, 3) != '039') {
  alert('TRUE');
} else {
  alert('FALSE');
}

This code will return 'TRUE' only when momo_no.substring(0, 3) is not equal to any of those specified values.

英文:

I have the below script which is failing to return the right condition (TRUE). What could be the problem?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var momo_no = 0759933091;
if (momo_no.substring(0, 3) != 075 || momo_no.substring(0, 3) != 070 || momo_no.substring(0, 3) != 074 || momo_no.substring(0, 3) != 077 || momo_no.substring(0, 3) != 078 || momo_no.substring(0, 3) != 076 || momo_no.substring(0, 3) != 039) {
  alert(&#39;FALSE&#39;);
} else {
  alert(&#39;TRUE&#39;);
}

<!-- end snippet -->

答案1

得分: 0

The substring function will not work with int values. It will only work with strings. Try the following code; it will work properly.

var momo_no = "0759933091";
if (momo_no.substring(0, 3) != "075" || momo_no.substring(0, 3) != "070" || momo_no.substring(0, 3) != "074" || momo_no.substring(0, 3) != "077" || momo_no.substring(0, 3) != "078" || momo_no.substring(0, 3) != "076" || momo_no.substring(0, 3) != "039") {
  alert('FALSE');
} else {
  alert('TRUE');
}
英文:

The substing function will not work int values. It will only work with string. Try below code, it will work properly.

var momo_no = &quot;0759933091&quot;;
if (momo_no.substring(0, 3) != &quot;075&quot; || momo_no.substring(0, 3) != &quot;070&quot; || momo_no.substring(0, 3) != &quot;074&quot; || momo_no.substring(0, 3) != &quot;077&quot; || momo_no.substring(0, 3) != &quot;078&quot; || momo_no.substring(0, 3) != &quot;076&quot; || momo_no.substring(0, 3) != &quot;039&quot;) {
  alert(&#39;FALSE&#39;);
} else {
  alert(&#39;TRUE&#39;);
}

答案2

得分: 0

以下是您要翻译的内容:

单个数字不能同时等于多个值所以请尝试使用&&来编写此代码希望它有所帮助
var momo_no = "0759933091";
if (
  momo_no.substring(0, 3) !== "075" &&
  momo_no.substring(0, 3) !== "070" &&
  momo_no.substring(0, 3) !== "074" &&
  momo_no.substring(0, 3) !== "077" &&
  momo_no.substring(0, 3) !== "078" &&
  momo_no.substring(0, 3) !== "076" &&
  momo_no.substring(0, 3) !== "039"
) {
  alert("FALSE");
} else {
  alert("TRUE");
}
英文:

Single number cannot be equal to multiple values at the same time, so try this code but with &&, hope it helps:

var momo_no = &quot;0759933091&quot;;
if (
  momo_no.substring(0, 3) !== &quot;075&quot; &amp;&amp;
  momo_no.substring(0, 3) !== &quot;070&quot; &amp;&amp;
  momo_no.substring(0, 3) !== &quot;074&quot; &amp;&amp;
  momo_no.substring(0, 3) !== &quot;077&quot; &amp;&amp;
  momo_no.substring(0, 3) !== &quot;078&quot; &amp;&amp;
  momo_no.substring(0, 3) !== &quot;076&quot; &amp;&amp;
  momo_no.substring(0, 3) !== &quot;039&quot;
) {
  alert(&quot;FALSE&quot;);
} else {
  alert(&quot;TRUE&quot;);
}


</details>



huangapple
  • 本文由 发表于 2023年6月8日 19:29:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定