2017-02-24 50 views
3

我正在通过FreeCodeCamp突变挑战。这是我必须做的:查找两个数组中的索引匹配

返回真,如果在数组的第一个元素的字符串包含 所有的数组的第二个元素串的字母。

例如,[“hello”,“Hello”]应该返回true,因为第二个字符串中的所有 字母都出现在第一个忽略大小写中。

参数[“hello”,“hey”]应返回false,因为字符串 “hello”不包含“y”。

最后,[“Alien”,“line”]应该返回true,因为“line”中的所有 字母都出现在“Alien”中。

这是我的解决方案。不幸的是,它不起作用,但我认为有可能解决这个问题。我的错误在哪里?

这里是我详细的注释代码:

function mutation(arr) { 

    //indexOf is case sensitive, so first we make all the elements in the array lowerCase. After that we use the lowerCaseArray instead of our original array 

var y = arr.join(" "); 
var x = y.toLowerCase(); 
var lowerCaseArray = x.split(" ") 

// This variable will contain the number of matches 

var matchCounter = 0; 

//The for loop picks a letter from the second element 
//(lowerCaseArray[1][i]) of an array and then we look 
//if a match in the first element of an array(lowerCaseArray[0] is found). 
//If there is a match, then the indexOf would return a number >=0. 
//In this case we add 1 to our matchCounter. 

for (i = 0; i < lowerCaseArray[1].length; i++) { 
    if(lowerCaseArray[0].indexOf(lowerCaseArray[1][i]) > 0) { 
    matchCounter+= 1; 

    } 

//Finally we compare the matchCounter length with the second 
//element of our array. If matchCounter >= the length of our 
//array, it means every letter in the second element was found 
//within the first element 

} 
return matchCounter >= arr[1].length; 

} 

mutation(["floor", "for"]); 

出于某种原因return lowerCaseArray[1][i];回报的“o”,但第二个元素的最后一个字母为“R”。在给出的例子中,matchCount等于2,但应该是3,因为有3个匹配。也许这是一个错误的部分。

+0

我运行了你的代码,它正确地遍历所有3个字母,'f','o'和'r'。你能再次运行并验证吗? –

+0

它的确如此,但它返回false,因为matchCounter等于2,而不是3。 –

回答

1

导致您的代码返回错误的结果该生产线是这一个:

if (lowerCaseArray[0].indexOf(lowerCaseArray[1][i]) > 0) { 

它忽略了一种可能性,即寻求字符可以在0位置。

>更改为>=可以使其正常工作。您的意见实际上表明它应该是>=,但您的代码使用>

还有一些其他地方的代码可以做得少一点错综复杂。请看下图:

function mutation(arr) { 
 

 
    //indexOf is case sensitive, so first we make all the elements in the array lowerCase. After that we use the lowerCaseArray instead of our original array 
 

 
    var lowerCaseArray = arr.map(function (str) { 
 
     return str.toLowerCase(); 
 
    }); 
 

 
    // the for loop checks each letter in the second string 
 
    // if any of its letters is not present in the first one, 
 
    // return false immediately 
 
    for (i = 0; i < lowerCaseArray[1].length; i++) { 
 
    if (lowerCaseArray[0].indexOf(lowerCaseArray[1][i]) === -1) { 
 
     return false; 
 
    } 
 
    } 
 
    
 
    // if the for loop completed without returning, then the strings pass the test. 
 
    return true; 
 
} 
 

 
console.log(mutation(["floor", "for"]));

0
if(lowerCaseArray[0].indexOf(lowerCaseArray[1][i]) > -1)? 
2

您需要检查的不平等-1,因为零0是字符串的有效指标。

if (lowerCaseArray[0].indexOf(lowerCaseArray[1][i]) !== -1) { 
//             ^^^^^^ 

function mutation(arr) { 
 
    var y = arr.join(" "), 
 
     x = y.toLowerCase(), 
 
     lowerCaseArray = x.split(" "), 
 
     matchCounter = 0, 
 
     i; 
 

 
    for (i = 0; i < lowerCaseArray[1].length; i++) { 
 
     if (lowerCaseArray[0].indexOf(lowerCaseArray[1][i]) !== -1) { 
 
      matchCounter += 1; 
 
     } 
 
    } 
 
    return matchCounter >= arr[1].length; 
 
} 
 

 
console.log(mutation(["floor", "for"]));

0

另外,您也可以使用regex。这也将为您节省匹配大小写的麻烦。

样品

function validate(arr){ 
 
    var regex_str = arr[0].replace(/[^a-z0-9 ]/gi, function(m){ return "\\" + m }) 
 
    var regex = new RegExp("^[" + regex_str + "]*$", "i"); 
 
    var valid = regex.test(arr[1]); 
 
    console.log(regex, "|", arr[1], "|", valid) 
 
    return valid 
 
} 
 

 
validate(["floor", "for"]) 
 
validate(["floor", "fora"]) 
 
validate(["heworld", "hello World "]) 
 
validate(["heworld", "hello World "]) 
 
validate(["heworldts(*) ", "hello World (test *)"])

+0

如果第一个字符串包含像'\'或']'这样的字符,就会中断。 – JLRishe

+0

@JLRishe感谢您指出。请检查更新。 – Rajesh

0

试试这一次匹配( '唱歌', '歌手')。

var match=function(a,b) 
{ 
    var nomatch=0; 
    var w1=a.toLowerCase(); 
    var word1=w1.split(''); 
    var string2=b.toLowerCase(); 
    console.log('word1 len : '+word1.length+' '+string2+' length : '+string2.length); 
     for(var i=0;i<word1.length;i++) 
    { 
     if(string2.indexOf(word1[i])==-1) 
     { 
      console.log('no match : '+b+' .indexOf('+word1[i]+')'); 
      nomatch +=1; 
     } 
     else 
     { 
      console.log('match : '+b+'.indexOf('+word1[i]+')'); 

     } 
    } 
    if(nomatch>0) 
    { 
     console.log(b+' does not have all characters of '+a); 
    } 
    else 
    { 
     console.log(b+' do have all characters of '+a); 
    } 

} 
match('hello','Hell');