Hi, I’ve just completed the Mutations challenge in Basic Algorithms. I have a quick question.
Why does this solution work:
function mutation(arr) {
for (let i = 0; i < arr[1].length; i++) {
if (arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase()) == -1) {
return false;
}
}
return true;
}
and this one doesn’t?
function mutation(arr) {
for (let i = 0; i < arr[1].length; i++) {
if (arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase()) == -1) {
return false;
} else return true;
}
}
The only difference is I’m putting “return true” inside an if else statement. The test that doesn’t pass is “mutation([“hello”, “hey”]) should return false.”
Why is this? Aren’t the two codes essentially the same thing?