The Mutations Algorithm Blues

The Mutations Algorithm Blues
0

#1

Hello,
I was wondering if anyone could nudge me in the right direction with this problem. I don’t wanna give up and look at solutions yet because I feel like I’m really close. I’m assuming that I’m missing something around the for-loop-section of my code because the only value that I get in my console is -1 for every indexOf() that I check within the loop. Thanks in advance for any responses!

<
function mutation(arr) {
  var stringZero = arr[0];
  var stringOne = arr[1];
  var lowerZero = stringZero.toLowerCase();
  var lowerOne = stringOne.toLowerCase();
  var firstLetters = lowerZero.split("");
  var secondLetters = lowerOne.split("");
  var arrZeroLetters = [];
  var arrOneLetters = [];
  arrZeroLetters.push(firstLetters);
  arrOneLetters.push(secondLetters);

  var storeIn;
  

  for (var i = 0; i < arrOneLetters.length; i++){

   
        console.log(storeIn);
        storeIn = arrZeroLetters.indexOf(arrOneLetters[i]);
        var seeIn = arrOneLetters[i];
        console.log(arrZeroLetters);
        console.log(arrOneLetters);
        console.log(seeIn);
        console.log(storeIn);

        if (storeIn == -1){
          return false;
        }
      
  }return true;
      
}
  


mutation(["u", "u"]);
>

#2

The main issue is that arrZeroLetters is an array of arrays instead of just an array, so when you write:

storeIn = arrZeroLetters.indexOf(arrOneLetters[i]);

storeIn will always be -1, so you will always return false.

Also, you can not check to see if an array is in another array like you are trying to do with indexOf.

Finally, the following line adds no value to your function as you never use it to determine what the function returns. You can get rid of this line:

var seeIn = arrOneLetters[i];

You actually do not need to create any arrays to solve this challenge. You start with an array with two elements. The first element is a string and the second element is a string.

Strings also have an indexOf function which works like the array version. Why not iterate through stringOne and see if you find a single letter of it which does not exist stringZero? When you find that instance of a missing letter, you can simply return false. If you make it through the entire 2nd element string without returning false, you can simply return true.


#3

The reason your indexOf is always returning -1 is because that’s what indexOf returns when the argument is not one of the array values. Your arrZeroLetters and arrOneLetters are arrays of arrays, so their contents are all arrays, never letters. You are comparing two arrays when you do arrZeroLetters.indexOf(arrOneLetters[i]);, but arrays don’t work like that. Comparing arrays only returns true when they are the exact same array, not when they are different arrays with the same contents.


#4

Thanks for the quick response! I had a feeling that I was committing redundancies but wasn’t quite sure/didn’t feel too comfortable about treating strings like I could arrays–so, I appreciate that hint!


#5

Thanks so much for that nudge-hint! I’ll get back at it…