前端Freecode camp

Mutations -- Freecodecamp

2017-03-25  本文已影响36人  Marks

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
上一篇下一篇

猜你喜欢

热点阅读