Closed. This question is not reproducible or was caused by typos。它当前不接受答案。
                        
                    
                
            
        
            
        
                
                    
                
            
                
                    想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                
                    2年前关闭。
            
        

    

我尝试计算字符串中的错误词,但是我的代码始终显示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

最佳答案

请找到以下快照,这可能有所帮助:)



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"));

关于javascript - JS坏话专柜,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53341570/

10-16 10:37