我尝试计算字符串中的坏词,但是我的代码始终显示0。这是怎么回事?
function checkWord(list){
var j = 0;
var badWords = ["war", "bomb"]; //bad word list
for(var i = 0; i<badWords.lenght; i++){
if( list.toLowerCase().search(badWords[i])) j++; //check is it bad word in sting if true j+1
}
return j;
}
console.log(checkWord("sfasdf dgfdfsg sdfsA bomb war"));
总是返回:0应该是:2
答案 0 :(得分:1)
请找到以下快照,这可能会有所帮助:)
function checkWord(list){
var j = 0;
var badWords = ["war", "bomb"]; //bad word list
badWords.forEach(word => {
list.includes(word) && j ++
})
return j
}
console.log(checkWord("sfasdf dgfdfsg sdfsA bomb war"));