英文:
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('FALSE');
} else {
alert('TRUE');
}
<!-- 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 = "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');
}
答案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 = "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");
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论