Mutations problem

Mutations problem
0.0 0

#1

I almost got this working, I just need to find out why it is return true when I input mutation([“hello”, “hey”]);

My code so far

function mutation(arr) {

  
  var arr1 = arr[0];
  var arr2 = arr[1];
  var res = arr1.toLowerCase();
  var res2 = arr2.toLowerCase();
  
  var sliced = res.split("");
  var sliced2 = res2.split("");
  
  
  for (var int = 0; int < arr2.length; int++) 
  {
      if (sliced.indexOf(sliced2[int]) >= 0)
        {
          return true;
        }
    
    else
    {
     return false; 
    }
    
  }
  
 
}

mutation(["hello", "hey"]);

#2

Your code is always returning on the first iteration of the for loop. It’s almost an accident that all tests except one pass.


#3

I’m not understanding what you mean.


#4

Suppose the for loop starts its first iteration. It starts with checking if sliced.indexOf(...) >= 0. If it’s true, the return true; statement runs. If not, the return false; statement runs.

Note that returning from inside a for-loop effectively stops the for-loop from running further. Now since inside your loop it’s only either return true; or return false;, it doesn’t get to iterate through the remaining elements in arr2.