且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查字符串是否包含另一个字符串的元素

更新时间:2022-11-17 13:45:24

'快速的棕色狐狸跳过懒狗'

算法:

你知道当你找到一个不在句子中的1个字母时,所有字母都不在句子中。

你知道所有字母都是当你在句子里找到每个字母的句子。



翻译:

'The quick brown fox jumps over the lazy dog'
Algorithm:
You know that all letters are not in a sentence when you have found 1 letter that is not in the sentence.
You know that all letters are in a sentence when you have found each letter in the sentence.

Translation:
set an answer variable to true
Check each letter in a loop
    if the letter is not in sentence
        answer is false
then after loop, check if answer is true or false and do what you want.
if answer is true
    bingo
else
    Bollocks


var alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
var input = prompt("Type in a message","");
var i;
for ( i = 0; i < alphabet.length; i++) 
{
  if(input.includes(alphabet[i]) == false) 
    break;
} 
if ( i == alphabet.length)
  alert("Bingo");
else 
  alert("Bollocks!"); 


试试这个,



Try this,

var occurencePositive = 0;
for(var index = 0; index <alphabet.lenght; index++){
    if(input.IndexOf(alphabet[index]) > -1){
       //increase counter when match is found in string.
       occurencePositive++;
    }
}

if(occurencePositive == alphabet.length - 1){
  alert("Bingo");
} 
else { 
  alert("Bollocks!");
}





希望这会有所帮助。



Hope this helps.