# Basic algorithm scripting exercise

Hello, all.

I am running into trouble with one of the exercises and I hope that one of you will help me to see what I am missing.

The exercise that I’m running into confusion with is here: Basic Algorithm Scripting: Mutations

The code below passes all but one of the tests and I am hoping for insight that will help me to think more clearly about suitable approaches to this problem. As always, any input will be greatly appreciated.

``````
function mutation(arr) {
let firstEl = arr[0].toLowerCase();
let secondEl = arr[1].toLowerCase();

for (var i = 0; i < secondEl.length; i++) {
if (firstEl.indexOf(secondEl[i]) !== -1) {
return true;
}
return false;
}
}
```
```

As one of the return statements will always be executed, your loop never goes past i = 0, because the return statement breaks out of the function

Maybe think again when you need to return one and when the other (can you return one of the two once a condition is met? Can you return the other only once everything has been checked?)

1 Like

With your insight in mind, I made a couple of changes and got it working as intended. Many, many thanks!

``````function mutation(arr) {
let x = arr[0].toLowerCase();
let y = arr[1].toLowerCase();

for (var i = 0; i < y.length; i++) {
if (x.indexOf(y[i]) === -1) {
return false
}
}
return true;
}
```
```
1 Like