Basic algorithm scripting: Mutations help!

Tell us what’s happening:
my code works for every condition except for mutation([“hello”, “hey”])… i returns true. How can that be ? anyone know what I’m doing wrong??
Your code so far


function mutation(arr) {
var elem1=arr[0].toLowerCase().split('');
var elem2=arr[1].toLowerCase().split('');

for(var i=0;i<elem2.length;i++){
if (elem1.includes(elem2[i])){

return true
}
else return false

}

return arr;
}

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/88.0.4324.150 Safari/537.36.

Challenge: Mutations

Link to the challenge:

When facing the issue of “This seems right, but it doesn’t work”, or actually any other problem, I always recommend to:

  1. Double check for potential spelling errors, or similar
  2. Use console.log() at different points, to find out where the error might be
  3. Go through the code yourself with an example.

After doing these three things, I will soon post the answer here :wink:

Answer:

  1. Everything seems correct, as far as spelling goes.
  2. The second step helped me find the solution:
    If you log elem2[i] within the if statement it only logs “h” to the console. The mistake is, that at the end of the if statement, you have a return, which is why the for loop doesn’t get executed again.

Hope this helped

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.