Mutations challenge (Solved)

Hi.

So, I’ve this code so far:

function mutation(arr) {  
 
  var arr0=arr[0].toLowerCase().split("");
  var arr1=arr[1].toLowerCase().split("");   
  
    for (i=0; i<arr1.length;i++){
      
      if (arr0.indexOf(arr1[i]) !== -1){
     
      return true;
     
    }
    
    else { 
      return false; 
    
    }
   
  }
  
}

Which validates all these test cases:

//mutation([“hello”, “Hello”]); //should return true.
//mutation([“zyxwvutsrqponmlkjihgfedcba”, “qrstu”]); //should return true.
//mutation([“Mary”, “Army”]); //should return true.
//mutation([“Mary”, “Aarmy”]); //should return true.
//mutation([“Alien”, “line”]); //should return true.
//mutation([“floor”, “for”]); //should return true.
//mutation([“hello”, “neo”]); //should return false.
//mutation([“voodoo”, “no”]); //should return false.

But not this one:

//mutation([“hello”, “hey”]); //should return false.

But if I change this !== -1 to this == -1 in my code, it validates that only one false test case, but not the rest of the above list. So, without any code or snippet, would you please be so kind to give me a hint of what’s wrong with code or approach?

Thank you!

But, I’ve added markers to the iteration to show me what it does, and they show me that it checks every letter from hey, with this result:

h=true
e=true
y=false

How come you say it only looks at the h only?

I’ve also tested the cases with this code (thinking that it compares all positions between strings) but it doesn’t work either.

  var arr0=arr[0].toLowerCase().split("");
  var arr1=arr[1].toLowerCase().split("");   
  
    for (j=0; j<arr0.length;j++){
    for (i=0; i<arr1.length;i++){
      
      if (arr0[j].indexOf(arr1[i]) !== -1){
     
      return true;
     
    }
    
    else { 
      return false; 
    
    }
   
  }
  }

Thank you for the example. As you can see, I’ve issues with iterators.

I realize the iteration stops once it finds the first true case, that’s why this one is actually failing, and the rest doesn’t. This one the case that must return false is the last character.

So i must iterate all positions first on arr1 no matter if they’re true or false, and then evaluate each if any is false on arr0? Am I closer?

I don’t understand why true must be outside the for. Could you help me with that please?

In the approach I referenced in my last reply, you put the return true outside the for loop. Why? Because you are iterating through the letters of arr1 and If any given letter is not found in arr0, then you will return false. Once you have finished iterating through all the letters of arr1 (after the for loop), it is impossible that any of the letters were not found, so you have to return true.

Let me show you another approach using the majority of your original code:

function mutation(arr) {  
  var arr0=arr[0].toLowerCase().split("");
  var arr1=arr[1].toLowerCase().split("");   
  
  var result = true;  // we make assumption we are going to find all the letters
  for (i=0; i<arr1.length;i++){
    if (arr0.indexOf(arr1[i]) === -1){
      result = false; // we did not find a letter, so result needs to be false 
      break; // stops for loop iteration because there is no need to continue iterating
    }
  }
  return result; // this will return the final value of result
}

In this solution and the one I described earlier, we make an assumption that true should be returned until proven otherwise. Once we do not find a letter, we must return false. If we find all the letters (after iterating through all of arr1), we can simply return true.

So, in natural language, the return true; outside the for, would be something like-

The if within the for loop is for all false results, and for what is left (true), it doesn’t need to be evaluated or iterated.

Is it like a default on a switch would work?

I know I’m a slow-noodle-head, but, I’ll take advantage of your kindness and ask until either no one answers me, or I understand it.

edit: I did get it. Thank you!