Mutations challenge question

This is driving me crazy…here is my original attempt to the “Mutations” challenge:

function mutation(arr) {
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].toLowerCase();
}

var arrTwo = arr[1];
for (var j = 0; j < arrTwo.length; j++) {
if (arr[0].indexOf(arrTwo[j]) > 0) {
return true;
} else {
return false;
}
}
}

Obviously this doesn’t work I’ve been told that I am only checking characters in arr[0] and not arr[1]. I had no idea how to check each character in arr[1] and didn’t want to pester him more as other people need help too. I just can’t understand what is happening here can someone explain it? Here is my solution:

function mutation(arr) {
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].toLowerCase();
}

var arrTwo = arr[1];
for (var j = 0; j < arrTwo.length; j++) {
if (arr[0].indexOf(arrTwo[j]) < 0)
return false;
}

return true;

}

I mean I found the solution myself with help of course, I just don’t understand it at all…and why my first attempt doesn’t work…

I can tell you why the first algorithm doesn’t work.

  • f (arr[0].indexOf(arrTwo[j]) > 0) is a bad test. indexOf() returns “The index of the first occurrence of the specified value; -1 if not found.”(MDN). So you are going to get 0 if char is at index 0. 2 if it is at index 2 & so on . you want to test to see if the char is present at all.
  • return true; is also bad. You will exit the function after finding the first match. You need to test ALL the letters in the 2nd word.

as far as not understanding, could you be more specific? What parts are giving you trouble? Which lines of code or concepts?

2 Likes

Hello there!

I’ve just completed the Mutation Challenge and here is my solution.

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

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

maybe you should at least explain your solution,
instead of just giving people code they can mindlessly copy paste. :frowning:

As my friend @diomed said, i should explain my code here.
First i just converted my both array elements to lower case by toLowerCase function. and store it in the same element.
Then i split my second element to an array with name myArr.

Then by using for loop, i am looking for whether the second elements letter’s exist in first element or not?

To do so I’m checking each word of second element one by one by indexOf function into first element which is arr[0]. indexOf function give a positive number (index number) if the value exist in arr[0] element. If the value does not exist in the element then it will give -1 value which will be less than 0. and if even one letter is not in first element then we have to give false as result.

So that’s how I completed my challenge. Hope this explanation will help you. Please let me know if you want to correct me or need any other help.

Happy Coding! :smile:

1 Like

I didn’t understand why if the second solution worked the first didn’t, because they looked exactly the same just in reverse order. But, thanks to your explanation of why the first attempt doesn’t work it made things clearer. I had just spent about 7 hours prior studying so I was pretty tired and hungry. Frustration was clouding my judgement, but I understand now thanks.

edit: to be more specific I didn’t understand that:

if (arr[0].indexOf(arrTwo[j]) > 0) {
return true;

would end the loop at the first instance of a number greater than 0 and return true rather than the solution which checked the characters up to “y” which would return false. I thought my first attempt would check all letters including the “y”.

1 Like