Mutations Solution: Question about the loop logic

Mutations Solution: Question about the loop logic
0.0 0


Hi all,
I’m fairly new to fcc, as I’ve only been working on the front end development cert for a couple of weeks now. I’ve taken a look at the code provided as the solution for this challenge, and its really similar to the one that i wrote here, except my code returns a true for [“hello”, “hey”] instead of a false. The solution given in the hint section says removing the else statement, and moving the “return true;” outside of the for loop will fix the issue, and it does, but I don’t understand why. Does anyone know what the difference in the logic is?

Your code so far

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


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

Link to the challenge:


Considering that return stops the function, your condition will check only one letter.

The return false; is correct here because once you get to a character that does not exist in the newArr1 then you want to stop, because there is no point going on.

However you want to return true if ALL the characters in newArr2 are in newArr1. You can only do so when you checked that all characters in newArr2 are in newArr1.

So, where should you return true; be?


Ohhhh, if I’m understanding you correctly, keeping the “return true;” in the for loop will stop the code as soon as it hits a character that matches , whereas moving it outside the loop will force the code to run through the whole string and thus find the mismatch?



If the loop does not return false, it will complete until i = newArr2.length then go to the next line that could be return true;