Implement the Mutations Algorithm - Implement the Mutations Algorithm

Tell us what’s happening:

I know the return keyword is the issue here, but I still feel like this code makes sense. I just don’t know how to get my return keyword out of the loop so it doesn’t end early.

Am I wrong? Is the solution truly to just take another approach or should I just think of a way to get rid of “return”?

Your code so far



function mutation(array) {
  const arrayItem1 = array[0].toLowerCase();
  const arrayItem2 = array[1].toLowerCase();
  
  for (const letter of arrayItem2) {
    if (arrayItem1.includes(letter)) { 
      console.log(letter)
      return true
    }
    else {
      return false
    }
  }
}


console.log(mutation(["hello", "hey"])) //false because first element should contain all letters of the second one

Your browser information:

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

Challenge Information:

Implement the Mutations Algorithm - Implement the Mutations Algorithm

1 Like

Hey there internet person,

You are returning way too early from your for loop. Without giving too much away, reconsider how you’ll scope your return statements. Does that make sense?

1 Like

can you think, when can you say for sure that not all the letters matcj? and when can you say for sure that all the letters match?

if you think of the logic you can also find out where to put the return

2 Likes

Thank you, glad to report back that I was able to implement my return later on! Took me painfully long to get there though.