Mutations challenge solution lets this sneak through

Mutations challenge solution lets this sneak through
0

#1

Hey there,

I’m very new to both coding and online learning so I don’t know if this observation is of any interest to anyone. Or if it’s the sort of thing to be posted on a forum like this.

I was working on the mutations challenge … it suggested using indexOf … that didn’t seem quite right to me for some reason, so i tried a few other angles but didn’t have any luck … I did a bit of read/search/asking and ended up with this solution

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

       var check = word1.indexOf(word2[i]);
  
             if(check === -1){
                          return false;
               }  

}
return true;
}

This solution passes the challenge, however, because it checks the indexOf(word2[i]) in iterations, when tested with the example below, it returned true. But all the letters ‘heh’ do not exist in ‘hello’- only two of them do. I think it tested the first ‘h’ in heh and returned true, and then the ‘e’ in heh and returned true, and then the second ‘h’ in heh, and returned true also. I think this is because each time it tested the ‘h’ it tested it against the single ‘h’ in hello. I don’t mean to be a stickler, but I don’t feel this really solves the problem. I’m sure there’s a more satisfying solution but I think I’ll move on to keep my momentum up and just keep it kicking around in my head as a quirk

mutation([“hello”, “heh”]);

cheers


#2

There’s this test case

mutation(["Mary", "Aarmy"]) should return true.

A function that also takes into account duplicate letters in the second argument would be interesting to make though.


#3

You’re right. It did offer the duplicate letter scenario - “Mary”, “Aarmy”. as a test case. I missed that.

I think I unconsciously swapped them around to - “Aarmy”, “Mary”, so that it would fit into my interpretation of the challenge.

Its fascinating how we tend to see what we expect to see.

Cheers