Mutations code not working as expected!

Tell us what’s happening:

So I think this is some good code. It seemed nicer than the hint’s explanation, but it doesn’t work! I tried rearranging the if statements repeatedly and double checking the element calls too, but to no avail. I’m certain I could have done the for loop like the hint, but it seemed unnecessary, but maybe my understanding of indexOf is off?
Advice would be lovely, thank you.

Your code so far

function mutation(arr) {
var array = arr;
   if (array[0].indexOf(array[1]) == -1) {
    return false;
  } else if (array[0].toLowerCase == array[1].toLowerCase) {
    return true;
  } else {
    return arr;
  }
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; CrOS x86_64 10323.62.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.184 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/mutations

Your function should always return true or false. It should not return the array.

1 Like

The reason it doesn’t work is that it doesn’t answer the question. It’s good code in a way, but it’s answering the wrong thing.

let’s look at what you’re checking in the if statements:


(array[0].indexOf(array[1]) == -1) checks if the second string can be found as a substring of the first, returning true if it cannot be. By substring I’m referring to the letters, in order, of string two is inside string one somewhere.

This isn’t what the question is asking for - the question asks if all characters in the second string can be found in the first string. Not in order.


(array[0].toLowerCase == array[1].toLowerCase) checks if the strings are identical once you change the case.

This isn’t what the question is asking for either! This is again checking the whole string and not looking at each character.


finally, you simply return arr - the other returns yield a bool, this should imo be made consistent with that.

2 Likes

Nice catch! I missed that entirely

Thank you for the thorough response, it was exceptional.