比较字符串

如果数组里的第一个字符串包含了第二个字符串中的所有字母,则返回 true。例如,["hello", "Hello"] 应该返回 true。因为在忽略大小写的情况下,第一个字符串包含了第二个字符串里出现的所有字母。["hello", "hey"] 应该返回 false。因为 hello 并不包含字符 y。最后,["Alien", "line"] 应该返回 true。因为 line 中的所有字母都出现在了 Alien 中

 1 function mutation(arr) {
 2 let origin = arr[0].toLowerCase();
 3 let target = arr[1].toLowerCase();
 4 for(let i =0;i<target.length;i++){
 5   if(origin.indexOf(target[i])===-1){
 6     return false;
 7   }
 8 }
 9 return true;
10 }
11 mutation(["hello", "hey"]);

 1.includes()

  • String.prototype.includes()方法用于判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false
1 str.includes(searchString[, position])
  • Array.prototype.includes()方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回false

1 arr.includes(valueToFind[, fromIndex])
 1 function mutation(arr) {
 2  let origin = arr[0].toLowerCase();
 3  let target = arr[1].toLowerCase();
 4  for(let i =0;i<target.length;i++){
 5    if(!origin.includes(target[i])){
 6      return false;
 7    }
 8  }
 9  return true;
10 }
11 mutation(["hello", "hey"]);

 2.  String.prototype.toLowerCase()会将调用该方法的字符串值转为小写形式,并返回一个新的字符串

1 str.toLowerCase()

3.  Array.prototype.every()方法测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值

1 arr.every(callback(element[, index[, array]])[, thisArg])
1  function mutation(arr) {
2    return arr[1].toLowerCase().split('').every(char => arr[0].toLowerCase().includes(char));
3  }
4  mutation(["hello", "hey"]);

 

posted @ 2021-05-17 11:25  icyyyy  阅读(145)  评论(0)    收藏  举报