【发布时间】:2020-07-18 02:22:39
【问题描述】:
我的代码应该检测货币符号,并根据结果执行代码,但代码在任何情况下都不会检测到“£”。以下是相关代码:
let requirements = [ "£", "$" ];
let mcontent = "$£50";
let y = 0;
for (let p = 0; p < requirements.length; ++p) {
if (mcontent.includes(requirements[p])) {
++y;
}
}
if (y == 1) {
//this is considered success, only ONE currency symbol was detected. If mcontent = '$50' or '£50', we should be here.
} else {
//this is considered failure, TWO or ZERO currency symbols were detected. In this scenario, I want the code to fail.
}
我知道这可能不是编写函数以完成我想要完成的任务的最佳方式,因此我愿意为我已有的内容提供更好的想法/修复。
【问题讨论】:
-
您期待什么?该字符串中有两个货币符号。
-
@hev1 我什至通过重写代码来回答这个问题而没有注意到????
-
@SenseiMunchkin 我无法重现该问题:jsfiddle.net/qkL3zcyp
-
console.log(y)在你的 if 语句之前,你会看到2,而不是 1。 -
@SenseiMunchkin 您的问题无法重现(在 nodejs 服务器上打印 2):repl.it/repls/WearableUnderstatedSets#index.js,您缺少详细信息,请尝试创建一个 minimal reproducible example,以便人们可以帮助调试您的代码.您也是说
mcontent是用户输入的,请确保您认为是用户输入的£的字符实际上与您的数组中的字符相同。
标签: javascript arrays for-loop if-statement