Help with Mutations challenge - Basic Algorithm Scripting


#1

Can anyone tell me why this code doesn’t work? It passes on every test except the [‘hello’, ‘hey’] one.

function mutation(arr) {
  var strArr=[arr[0].toLowerCase(), arr[1].toLowerCase()];
  for (var i = 0; i < strArr[1].length; i++) {
    if (strArr[0].indexOf(strArr[1][i]) < 0) {
      return false;
    }
       return true;
    }
}

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

I have looked through the forum for solutions and they all seem to be the same as my code above. Sorry if I’ve missed something simple. but I would appreciate being pointed in the right direction.

Thank you.


#2

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

You’re returning too early. Currently your code will return true or false right at the for-loop’s first iteration (in effect you’re only testing the first element of the array in question, ignoring the rest). What you want is to return true after the loop.


#3

Thanks for cleaning up my code and for the response to my question. I didn’t catch the early return. Thanks again for responding so quickly.