# Basic Algorithm Scripting : Mutations Help,Please

Basic Algorithm Scripting : Mutations Help,Please
0

#1

Tell us what’s happening:
Hello, is there anything wrong with my code, it ticked all of items on the checklist but one:
“hello”,“hey”

``````function mutation(arr) {
firstString=arr[0].toLowerCase();
secondString=arr[1].toLowerCase().split('');
for(var index=0;index<secondString.length;index++){
if(firstString.indexOf(secondString[index])===-1){
return false;
}
return true;
}

}

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

Your Browser User Agent is: ```Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.91 Safari/537.36```.

https://www.freecodecamp.org/challenges/mutations``````

#2

Well the loop doesn’t complete it’s thing.
for example the first time the loop runs it take the first letter of “hey” which is “h” and check if the letter exists in the first word “hello” in this case and it does so the check fails and to goes to “return true” which stop the whole function and return true without it checking “e” or “y” so what you need to do is instead of just return false or true on the spot you make a variable that will hold that value and after the loop is finished you return that variable I’m gonna leave you with this for now and let try to do it yourself in case you couldn’t just ask

#3

It is pure coincidence the other test cases were correct. Your for loop only iterates one time (only the first index of secondString is evaluated by the if statement). It only iterates one time, because during the first iteration your if statement either returns true or false and then immediately exits the function. The return statement does not come back to the function to complete the for loop once the function is exited. A simple fix for your current code is the move the return true; line outside the for loop. This will work, because if you iterate through the entire secondString string, then you know all the letters were in firstString. If one letter would not have been in firstString, then your code would have returned false and that would have ended the execution of the function.

#4

Ooh, I see it!! Thank you! I’m under this misconception that everything has to be inside the for loop and curly thingy needs to come right after the parentheses. Silly me.