“如果数组的第一个元素中的字符串包含数组第二个元素中字符串的所有字母,则返回true。” 出于某种原因,除了([“hello”,“hey”])之外,我可以通过其他任何检查点。 有小费吗?
function mutation(arr) { var firstIndex = arr[0].toLowerCase(); var secondIndex = arr[1].toLowerCase(); for(var i = 0; i < arr.length; i++) { if(firstIndex.indexOf(secondIndex.charAt(i)) !== -1) { return true; } else { return false; } } return arr; } mutation(["hello", "hey"]);"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 some reason I can pass every other checkpoint except (["hello", "hey"]). any tips?
function mutation(arr) { var firstIndex = arr[0].toLowerCase(); var secondIndex = arr[1].toLowerCase(); for(var i = 0; i < arr.length; i++) { if(firstIndex.indexOf(secondIndex.charAt(i)) !== -1) { return true; } else { return false; } } return arr; } mutation(["hello", "hey"]);最满意答案
这个想法是检查secondIndex每个字符,看它是否包含在firstIndex 。 因此,这里的逻辑将是:对于secondIndex任何字符,如果该字符不在firstIndex ,则返回false(结束函数)。 如果在检查完所有字符后函数没有结束,你知道在firstIndex中找到了每个字符,然后你可以返回true。
function mutation(arr) { var firstIndex = arr[0].toLowerCase(); var secondIndex = arr[1].toLowerCase(); for(var i = 0; i < secondIndex.length; i++) { if(firstIndex.indexOf(secondIndex[i]) === -1) { return false; } } return true; } mutation(["hello", "hey"])The idea is to check every character in secondIndex to see if it is contained in firstIndex. So the logic here would be: for ever character in secondIndex, if the character is not in firstIndex, return false (ending the function). If the function doesn't end after checking all the characters, you know that each character was found in firstIndex, and you can then return true.
function mutation(arr) { var firstIndex = arr[0].toLowerCase(); var secondIndex = arr[1].toLowerCase(); for(var i = 0; i < secondIndex.length; i++) { if(firstIndex.indexOf(secondIndex[i]) === -1) { return false; } } return true; } mutation(["hello", "hey"])更多推荐
发布评论