Challenge Mutation - JS

Tell us what’s happening:
Hi everyone,
I’ve been trying to find out where was my mistake but I can’t find it.
My code works for every differents case except for :

mutation([“Hello”, “Hey”]);

The point of the exercise is to compare the first and second string inside the array and see if they have the same letters so far. If one is different it has to return false, otherwise, true.
Thank you for ur help !
Your code so far


function mutation(arr) {
  var arr1 = arr[0].toLowerCase();
  var arr2 = arr[1].toLowerCase();
  for(let i = 0; i<arr2.length ; i++){
    if(arr1.indexOf(arr2[i])< 0){
      return false;
    }  
    return true;    
  }    
}


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

Link to the challenge:
I can’t since it’s my first post

Edit: correct some mistakes about my explaination

You may be missing the point. You are supposed to validate that all the letters of the second element appear in the first element.

For example, if the following was the array passed to the function:

["tacks", "cat"]

then you would return true, because the letters c, a, t are found somewhere in the word tacks.

In the case of:

mutation(["Hello", "Hey"]);

the y is not found in the word Hello, so it should return false.

The tests for the challenge were actually poorly written. You actually just got lucky passing the other tests, because your code only looks at the first letter of the second element and checks if it is in the first element or not. If it is not, you return false and if it is, then you return true. You never iterate past the first letter of the second element.

Yes that’s what I just noticed too.

I’m still trying to understand why it stops after the first character, I tried with differents value the problem never changed.

the issue is that once a return statement is executed the function stops, and you have two return statements in the loop of which one of the two is always executed

you need to rethink where to put the return statements

As soon as your code executes a return statement, the function exists and does not come back (not even to finish iterating a for loop).

Ahaha thank you, it’s been like 30min that I’m looking at my loop or my condition trying to figure out what I did wrong there.
I did not notice the 2nd return wasn’t placed out of my loop.
Fresh eyes sometimes…

Thank you

@StefanYYC There is no reason to post your passing code. In fact, we would prefer you did not. We are just glad you figured it out on your own.