Can't solve the Mutations challenge from Basic Algorithm Scripting

What is wrong with this code? Can’t solve the ones that should return false. Adding an else with “return false” is not working.
This is the test without return false:
// running tests

mutation(["hello", "hey"])

should return false.

mutation(["hello", "neo"])

should return false.

mutation(["voodoo", "no"])

should return false.

mutation(["ate", "date"]

should return false.

mutation(["Tiger", "Zebra"])

should return false. // tests completed

And if I add the else I fail with the ones that need to return true…

Your code so far


function mutation(arr) {
  for(var i = 0; i < arr.length; i++){
    for  (var j = 0 ; j < arr[i].length; j++){
      var letras  = arr[i][j];
      var lC = letras.toLowerCase();
 
      if (arr[0].indexOf(lC) > 0 ) {
        return true;
      }
    }
  }
  return arr;
}

mutation(["Mary", "Army"]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36.

Challenge: Mutations

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations

You’re iterating over both arrays (outer loop) & checking both against the first (arr[0].indexOf(lC)). You’re supposed to be checking the second value in the array against the first, you don’t need to iterate to get those two string, that’s the wrong thing to do: it’s just complicating things and confusing you, you literally just need arr[0] and arr[1]. Then you can iterate over the characters.

And you’re returning in the loop: as soon as that condition is true, your function exits.

So for the first check.

arr[i][j] is M
lC is m
"Mary".indexOf("m") is -1 (it's not in the string)

Second check

arr[i][j] is a
lC is a
"Mary".indexOf("a") is 1
1 is greater than 0
return true
loop ends
function exits
1 Like