# Mutation algorithm JS only one error with my code

Tell us what’s happening:
For pass this challenge I have only one error with test, which is [“hello”, “hey”] .
Other than that works fine. I don’t know what I did wrong.
I know my code is far away from solutions on fcc but I want to figure out how I make my code works! It has been just a week so I consider myself as a totally beginner so please understand my ignorance ! I would really appreciate your help! Thanks!

``````
function mutation(arr) {
var str1=arr[0].toLowerCase();
var str2=arr[1].toLowerCase();
var splStr1=str1.split("");
var splStr2=str2.split("");
if(splStr1.indexOf(...splStr2)!=-1){
return true;
} else {
return false;
}
}

mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"]);
console.log(mutation(["hello", "hey"]));
``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36`.

Challenge: Mutations

If I am not mistaken, you only check against the first character of the second string, which by coincidence happens to work out for all test but the first … since there the odd character is on index 2 …
` if (splStr1.indexOf(...splStr2) != -1) {`

1 Like

So their solution is

``````function mutation(arr) {
let hash = Object.create(null);

arr[0].toLowerCase().split('').forEach(c => hash[c] = true);

return !arr[1].toLowerCase().split('').filter(c => !hash[c]).length;
}

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

I would probably do it this way so that you can stop searching after the first time you find a character that is not in the hash as I believe the filter function would keep going even if you already found a character not in the first array item:

``````function mutation(arr) {
let hash = Object.create(null);
arr[0].toLowerCase().split('').forEach(c => hash[c] = true);
let searchFor = arr[1].toLowerCase().split('');
for (let i = 0; i < searchFor.length; i++) {
if (!hash[searchFor[i]]) {
return false;
}
}
return true;
}
``````
1 Like