【发布时间】:2018-04-06 03:07:30
【问题描述】:
我正在检查字符串输入是否包含任何字符串数组。它通过了大部分测试,但没有通过以下测试。
谁能分解我的代码为什么它不能正常工作?
function checkInput(input, words) {
var arr = input.toLowerCase().split(" ");
var i, j;
var matches = 0;
for(i = 0; i < arr.length; i++) {
for(j = 0; j < words.length; j++) {
if(arr[i] == words[j]) {
matches++;
}
}
}
if(matches > 0) {
return true;
} else {
return false;
}
};
checkInput("Visiting new places is fun.", ["aces"]); // returns false // code is passing from this test
checkInput('"Definitely," he said in a matter-of-fact tone.',
["matter", "definitely"])); // returns false; should be returning true;
感谢您的宝贵时间!
【问题讨论】:
-
不是案例问题
-
你为什么不用正则表达式?
-
很多非正则表达式的简单方法可以做到这一点。
if (words.some(word => input.includes(word))) {/*do a thing*/}在第一场比赛后停止。
标签: javascript arrays string