Basic Algorithm Scripting Mutations

Hey Everyone!

Been working on this for the last 10 minutes, and can’t figure out why one case doesn’t work. Trying not to use the hints!

function mutation(arr) {
  let word1 = arr[0].toLowerCase();
  let word2 = arr[1].toLowerCase().split('');

  // Let's use indexof
  let count = 0;
  for (var i = 0; i < word2.length; i++) {
    if (word1.indexOf(word2[i]) >= 0) {
      return true;
    return false;

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

Let me know if I am missing something!!!

I did take out the .split() I didn’t need it

So consider what your loop is doing: at the first iteration, where you have ‘h’ in word1 and ‘h’ in word2, the if check is hit – what happens? Does the loop continue?

I might consider flipping your logic around – the first time you find a letter that DOESN’T match, return false. When do you think you should return true?

That’s exactly what I did! Still didn’t work… then I had to think scope… Thanks for the help!

Not so much a question of scope as it is a question of returning earlier than you might think. But hope it helped. :slight_smile:

sure did! I got it finally after realizing it!