Implement the Mutations Algorithm - Implement the Mutations Algorithm

The only issue I’m having is that array 3, 7, 10 and 11 are not passed through correctly and I’m unsure of where to go from here. Please help.

const array1 = ["hello", "hey"];
const array2 = ["hello", "Hello"];
const array3 = ["zyxwvutsrqponmlkjihgfedcba", "qrstu"];
const array4 = ["Mary", "Army"];
const array5 = ["Mary", "Aarmy"];
const array6 = ["Alien", "line"];
const array7 = ["floor", "for"];
const array8 = ["hello", "neo"];
const array9 = ["voodoo", "no"];
const array10 = ["ate", "date"];
const array11 = ["Tiger", "Zebra"];
const array12 = ["Noel", "Ole"];

function mutation(array) {
  const str1 = array[0].toLowerCase();
  const str2 = array[1].toLowerCase();
  for (let char of array) {
    if (str1.includes(char) === str2.includes(char)) {
      return "true"
    } else {
      return "false"
    }
  }
}

console.log(mutation(array3));
console.log(mutation(array7));
console.log(mutation(array10));
console.log(mutation(array11));

What exactly does the return keyword do? Specifically, can a function keep running after return?

Yes. The function stops its execution once a specified value is returned.

So how many characters will be compared ever?

function mutation(array) {
  const str1 = array[0].toLowerCase();
  const str2 = array[1].toLowerCase();
  for (let char of str2) {
    if (str1.includes(char)) {
      return "true"
    } else {
      return "false"
    }
  }
}

I changed my code which seemed to fix everything except for the [“hello”, “hey”] array.

You still have the same core problem though.

How many times will this loop run?

If I put console.log(char) with str2 = “hey” it will run 3 times. I realized that if the first letter in both strings are the same, it always returns true despite one string having “y” but not the other.

it run once, at the first iteration one return or the other will execute, and that stops the function and also the loop

This is exactly the problem. The return statement tells the loop to immediately give up during the first loop iteration and stop checking.

Got it! I switched to the for loop instead of for...of loop and used the includes() method to help find the false statements. Another mistake was adding " " around true and false statements after calling a return. Thank you so much for the help!