Help with Mutation Algorithm!

Help with Mutation Algorithm!
0.0 0


I am trying to check whether the second string in the the array has all the letters that are included in the first string of the given array.

function mutation(arr) {

arr = {
   return val.toLowerCase();
 for (var i = 0; i < arr[1].length; i++) { 
      if (arr[0].indexOf(arr[1].charAt(i), 0 <= 0) == -1) {
        return false;
      } else {
        return true;


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

My code works for checking whether the second string has a different letter than in the first string but it doesn’t work if the given array had strings such as
mutation(["hello", "Hello"]);
mutation(["floor", "for"]);


First, your for loop is only going to have one iteration, because in the first iteration where i=0, you use a return statement in the if and else blocks. Once a return statement is executed, the function is immediately exited and returns whatever you specify after the return.

Second, what do you think the following evaluates to in the first iteration and why do you think that? What are you trying to accomplish with the if statement in general. If you can answer those questions, then I can point you in the correct direction.

arr[0].indexOf(arr[1].charAt(i), 0 <= 0)


First of all, thank you for the reply. So, that line of code evaluates to to false, and I just realized how can it do that if the variable i only goes through one iteration. In the first iteration i = 0, so, that line of code is checking whether arr[0] (the first string) has the first character in arr[1] (the second string). -1 means it is not there so it is checking if the character is there or not. But, the first character of the second string(“hey”) in the given argument is “h” which indeed is in the first string so it should be returning true not false. So I am not really sure why it does that. The if/else statement was used to check whether the second string has a character that is not found in the first string. Kind of stuck here.

arr[0].indexOf(arr[1].charAt(i), 0 <= 0)


indexOf has two possible parameters. The first one is the value you are searching for, which in your code is arr[1].charAt(i). The second (optional) parameter is the index where you want to start. If this is not specified, it starts at 0. The problem is instead of specifying an integer, you have the following expression:

0 <= 0

Since 0 is less than or equal to 0, this evaluates to true. It appears (someone can correct me if I am wrong here) that JavaScript is taking this true value and converting it to the number 1, so basically in the first iteration of the for loop you have:

arr[0].indexOf(arr[1].charAt(0), 1)

and since arr[1].charAt(0) is the string “h” you are trying to find the first index where the letter “h” appears in “hello” (arr[1]) STARTING at index 1, you are only looking at “ello”. Since the letter “h” is not in “ello” it returns -1. So when your if statement is asking if -1 == -1 which is true which causes your solution to return false;

Changing your if statement to:

if (arr[0].indexOf(arr[1].charAt(i),0) == -1) {

will cause your code to return true in the first iteration. This will not solve the challenge, because you really only want to return true after iterating through the entire array.

You need to rethink your logic. One way to think about the problem is during each iteration, ask if the current letter of arr[1] is NOT present in arr[0], so you are checking if what is returned from the indexOf is less than 0. If it is, then return false. You do not need an else, because you do not want to do anything inside the for loop if the letter is found in arr[0]. Then after the for loop ends, you know you can safely return true, because it would have already returned false if a letter was not found.


Thank you so much for the tips and information. I will definitely keep this in mind. Do you have tips that you can provide concerning how to approach and solve an algorithm?

  1. Break down a problem into small pieces and figure out a solution for each smaller pieces first. Then it is just a matter of putting the smaller solutions together to solve the original problem.

  2. Make sure you try to write out your algorithm (steps) in plain language first before writing any code.

One of the issues you faced during this specific challenge (non-algorithm related) was that you were not correctly using the function indexOf. Make sure to read all the documentation and try to understand all the examples given on the functions, so you know how to properly implement them.


Alright, thank you again for the help!