Mutations -- Freecodecamp

Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.
For example, ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring case.
The arguments ["hello", "hey"] should return false because the string "hello" does not contain a "y".
Lastly, ["Alien", "line"], should return true because all of the letters in "line" are present in "Alien".
TEST☟
mutation(["hello", "hey"]) should return false.
mutation(["hello", "Hello"]) should return true.
mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"]) should return true.
mutation(["Mary", "Army"]) should return true.
mutation(["Mary", "Aarmy"]) should return true.
mutation(["Alien", "line"]) should return true.
mutation(["floor", "for"]) should return true.
mutation(["hello", "neo"]) should return false.
mutation(["voodoo", "no"]) should return false.

//wrong.  arr[0].toLowerCase().indexOf(arr[1].toLowerCase())!==-1只能判断arr[1].toLowerCase()整个字符是否在arr[0].toLowerCase()中
function mutation(arr) {
  if(arr[0].toLowerCase().indexOf(arr[1].toLowerCase())!==-1){
    return ture;
  }else {
    return false;
  }  
}
mutation(["hello", "hey"]);```

//有问题??
//return false; 位置问题,需要放在for循环后才符合逻辑
function mutation(arr) {
var target = arr[0].toLowerCase();
var test = arr[1].toLowerCase();
for (var i = 0; i < test.length; i++) {
if (target.indexOf(test[i])!==-1) {
return true;
}
return false;
}
}
mutation(["hello", "hey"]);//true```

function mutation(arr) {
  var test = arr[1].toLowerCase();
  var target = arr[0].toLowerCase();
  for (var i=0;i<test.length;i++) {
    if (target.indexOf(test[i]) < 0){
      return false;
    }    
  }
  return true; 
}
mutation(["hello", "hey"]);```

copy from https://github.com/freeCodeCamp/freeCodeCamp/wiki/Algorithm-Mutations
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,789评论 0 23
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,837评论 18 139
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,766评论 0 33
  • 刚学习水彩没多久,画的不怎么好,大家凑合着看看
    斋忧_阅读 200评论 2 0
  • 3月底的时候体重还在82kg徘徊两个月后的今天已经稳定在74.5kg早上最轻的时候可以到73.6Kg我开玩笑说这是...
    承谦阅读 214评论 1 0