Implement the Mutations Algorithm - Implement the Mutations Algorithm

Tell us what’s happening:

I’m unable to get the console.log test to consistently log the appropriate letters of the second string, specifically for the last two mutations shown. In addition, the returned “true” or “false” values, seem to be incorrect for those also.

This is what is being logged to the console:

h
e
l
l
o
true
h
false
l
false

Thanks in advance for your help!

Your code so far

function mutation(array) {
  const firstString = array[0].toLowerCase();
  const secondString = array[1].toLowerCase();
  for (let char of secondString) {
    console.log(char);
    if (!firstString.includes(secondString)) {
      return false;
    }
  }
  return true;
}

console.log(mutation(["hello","Hello"]));
console.log(mutation(["hello","hey"]));
console.log(mutation(["Alien","line"]));

Your browser information:

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

Challenge Information:

Implement the Mutations Algorithm - Implement the Mutations Algorithm

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-mutations/af2170cad53daa0770fabdea.md at main · freeCodeCamp/freeCodeCamp · GitHub

what are you checking with this? does this check changes for each loop iteration?

Hi ILM! I’m checking if the firstString doesn’t have a specific character from the secondString, looping through each character of the secondString until I find one that doesn’t exist in the firstString. If one doesn’t exist, the loop & function returns false. If all characters from the SecondString exist in the firstString after completing the loop, the function returns true.

What do you mean by “does this check changes”?

which of the variables in that line is the specific character? is it maybe firstString? that is the whole string tho, is it maybe secondString? that is the whole other string

you do this, but then you never use char, so what’s the point of the loop?

Ah, my understanding is that this type of “for … of” loop is structured so that includes() checks all of the firstString for the characters in the secondString, starting with secondString[0] all the way through until the entire secondString has been checked. Is that incorrect? If so, what syntax could I use there?

includes is going to check what you tell it to check, and what you are checking is exactly firstString.includes(secondString), the values of firstString and secondString are those from the function arguments

what is char? why have you created this loop? you never use char

Thank you! I replaced secondString with char in the if statement, per your comment above. :slight_smile:

do you understand what the difference is between this and the previous code?

Yes, I do. :slight_smile: I had assumed that “char” was implied/referenced by putting “SecondString” where it was. But after doing further research I now understand why that is not so.