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循環(huán)后才符合邏輯
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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,055評論 0 23
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,544評論 19 139
  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,905評論 0 33
  • 剛學(xué)習(xí)水彩沒多久,畫的不怎么好,大家湊合著看看
    齋憂_閱讀 209評論 2 0
  • 3月底的時候體重還在82kg徘徊兩個月后的今天已經(jīng)穩(wěn)定在74.5kg早上最輕的時候可以到73.6Kg我開玩笑說這是...
    承謙閱讀 262評論 1 0

友情鏈接更多精彩內(nèi)容