您好,我正在尝试用javascript做一个简单的匹配游戏。
如果用户以包含word_tmp中每个字符串的任何方式插入文本president goes crazy
,则word_tmp将变为true,如果他错过了一个字符串,则它将变为false。
word_tmp = ['president', 'goes', 'crazy'];
// string 1 contains the president, goes and crazy at one string
string1 = 'president goes very crazy'; // should output true
// string 2 doesn't contain president so its false.
string2 = 'other people goes crazy'; // should output false
我该怎么做?
最佳答案
试试这个:
var word_tmp = ['president', 'goes', 'crazy'];
var string1 = 'president goes very crazy';
var isMatch = true;
for(var i = 0; i < word_tmp.length; i++){
if (string1.indexOf(word_tmp[i]) == -1){
isMatch = false;
break;
}
}
return isMatch //will be true in this case