Implement the Mutations Algorithm - Implement the Mutations Algorithm

Tell us what’s happening:

only missing the first requirement to complete it.

Cant seem to find what is wrong. Not sure why the includes() method is finding an “y” inside “hello”.

Your code so far

function mutation(arr) {
  let fstStr = arr[0].toLowerCase();
  let sndStr = arr[1].toLowerCase();
  let match = 0;
  for (let char of sndStr) {
    if (fstStr.includes(char)) {
      match++;
      if (match = sndStr.length) {
        return true
      } else {
        return false
      }
    } else {
      match = 0;
      return false
    }
  }
}

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

Your browser information:

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

Challenge Information:

Implement the Mutations Algorithm - Implement the Mutations Algorithm

what is this doing? can you explain that?

To check if the second string is contained inside the first one I used the variable “match” to count how many letter match and if the number of matching letter were equal to the second string it would return true and if not, return false

are you sure that you are comparing the two values there?

just tested without it and clearly it was an unnecessary complication :laughing:

the result ended up being the same removing that condition

do you understand why this is not a comparison tho?

now I do thanks to your heads up :+1:

function mutation(arr) {
let fstStr = arr[0].toLowerCase();
let sndStr = arr[1].toLowerCase();
for (let char of sndStr) {
if (fstStr.includes(char)) {
return true
} else {
return false
}
}
}

console.log(mutation([“hello”, “hey”]));

still I dont understand why the includes() finds “y” inside “hello”

You need to use console.log() inside your function to troubleshoot.

You need to confirm that your variables are what you expect.

You need to confirm that your loop checks every character.

now, do you know what happens when a return is executed?

thanks for the tip “You need to confirm that your loop checks every character.”

the “if” was cheking it and returning a boolean every part of the “for” loop.

Just took the “if” out of the for loop and it worked

How can you use console.log() to confirm that your loop is checking every character in the second string?

You need to answer this question:

the “for” loop was checking every letter of the string but having the condition for true or false being checked inside the “for” loop caused it to be true as soon as the first letter matched so I took the “if” out of the loop and the function worked

So it’s working now, you’ve passed the tests?

So you see how it was not checking every letter? It’s not because of the if though.

code removed by moderator

this is the perfect solution

hi @zaccheus

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge. How to Help Someone with Their Code Using the Socratic Method

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You should move “return true” outside of “if” statement. because this loop stopped immediately after match tow letters.
I used “continue” instead of return and it worked.