# Mutations challenge (Solved)

Hi.

So, I’ve this code so far:

``````function mutation(arr) {

var arr0=arr.toLowerCase().split("");
var arr1=arr.toLowerCase().split("");

for (i=0; i<arr1.length;i++){

if (arr0.indexOf(arr1[i]) !== -1){

return true;

}

else {
return false;

}

}

}
``````

Which validates all these test cases:

//mutation([“hello”, “Hello”]); //should return true.
//mutation([“zyxwvutsrqponmlkjihgfedcba”, “qrstu”]); //should return true.
//mutation([“Mary”, “Army”]); //should return true.
//mutation([“Mary”, “Aarmy”]); //should return true.
//mutation([“Alien”, “line”]); //should return true.
//mutation([“floor”, “for”]); //should return true.
//mutation([“hello”, “neo”]); //should return false.
//mutation([“voodoo”, “no”]); //should return false.

But not this one:

//mutation([“hello”, “hey”]); //should return false.

But if I change this `!== -1` to this `== -1` in my code, it validates that only one false test case, but not the rest of the above list. So, without any code or snippet, would you please be so kind to give me a hint of what’s wrong with code or approach?

Thank you!

The problem with the code above is that in your first iteration of the for loop, you return true or false depending on the evaluation of the if statement condition.

In the example of

``````mutation([“hello”, “hey”]);
``````

your code only looks at the “h” in “hey” and attempts to get the index of the string “hello” which is equal to “h”. Since it finds the “h” at index 0 of “hello”, it returns true and does not continuing iterating through the array.

1 Like

But, I’ve added markers to the iteration to show me what it does, and they show me that it checks every letter from hey, with this result:

h=true
e=true
y=false

How come you say it only looks at the h only?

I’ve also tested the cases with this code (thinking that it compares all positions between strings) but it doesn’t work either.

``````  var arr0=arr.toLowerCase().split("");
var arr1=arr.toLowerCase().split("");

for (j=0; j<arr0.length;j++){
for (i=0; i<arr1.length;i++){

if (arr0[j].indexOf(arr1[i]) !== -1){

return true;

}

else {
return false;

}

}
}
``````

The code you just posted above is different than what you originally posted. The new code has two for loops and the original has only a single for loop.

If I put console.log statements into your originally posted solution, you can see what is happening.

``````function mutation(arr) {
var arr0=arr.toLowerCase().split("");
var arr1=arr.toLowerCase().split("");

for (i=0; i<arr1.length;i++){
console.log('getting ready to enter if statement with arr1['+i+'] = ' + arr1[i]);
if (arr0.indexOf(arr1[i]) !== -1){
console.log('found ' + arr1[i] + ' in ' + arr + ' so return true');
return true;
}
else {
console.log('did not find ' + arr[i] + ' in ' + arr + ' so return false');
return false;
}
console.log(); // blank line to separate iterations
}
}
``````

mutation([“hello”, “hey”]); yields the following:

getting ready to enter if statement with arr1 = h
found h in hello so return true

mutation([“Alien”, “line”]); yields the following:

getting ready to enter if statement with arr1 = l
found l in Alien so return true

mutation([“hello”, “neo”]); yields the following:

getting ready to enter if statement with arr1 = n
did not find n in hello so return false

1 Like

Thank you for the example. As you can see, I’ve issues with iterators.

I realize the iteration stops once it finds the first true case, that’s why this one is actually failing, and the rest doesn’t. This one the case that must return false is the last character.

So i must iterate all positions first on arr1 no matter if they’re true or false, and then evaluate each if any is false on arr0? Am I closer?

One approach to this challenge, is to iterate through and have a single if statement which is:

``````    if (arr0.indexOf(arr1[i]) === -1){
return false;
}
``````

The above will keep iterating through all the letters of the arr1 array until it finds a letter that is not in arr0. If the entire arr1 is iterated without returning a false, then you can simply put a return true after the for loop has ended. You can return true, because you know there was no false condition.

You could think of the above approach as:

Assume true will be return unless a letter is not found, which will result returning false.

1 Like

I don’t understand why true must be outside the for. Could you help me with that please?

In the approach I referenced in my last reply, you put the return true outside the for loop. Why? Because you are iterating through the letters of arr1 and If any given letter is not found in arr0, then you will return false. Once you have finished iterating through all the letters of arr1 (after the for loop), it is impossible that any of the letters were not found, so you have to return true.

Let me show you another approach using the majority of your original code:

``````function mutation(arr) {
var arr0=arr.toLowerCase().split("");
var arr1=arr.toLowerCase().split("");

var result = true;  // we make assumption we are going to find all the letters
for (i=0; i<arr1.length;i++){
if (arr0.indexOf(arr1[i]) === -1){
result = false; // we did not find a letter, so result needs to be false
break; // stops for loop iteration because there is no need to continue iterating
}
}
return result; // this will return the final value of result
}
``````

In this solution and the one I described earlier, we make an assumption that true should be returned until proven otherwise. Once we do not find a letter, we must return false. If we find all the letters (after iterating through all of arr1), we can simply return true.

So, in natural language, the `return true;` outside the `for`, would be something like-

The `if` within the `for` loop is for all `false` results, and for what is left (`true`), it doesn’t need to be evaluated or iterated.

Is it like a `default` on a `switch` would work?