Basic Algorithm Scripting: Mutations question

Hello all!

I have a quick question: here’s the code that I wrote to solve this exercise:


function mutation(arr) {

for(var i = 0; i < arr[1].length; i++){
  console.log(arr[1][i])
  if(arr[0].toLowerCase().includes(arr[1][i].toLowerCase())){
  } else {
    return false;
  }
}
return true;
}

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

I originally had it like this:

function mutation(arr) {

for(var i = 0; i < arr[1].length; i++){
  console.log(arr[1][i])
  if(arr[0].toLowerCase().includes(arr[1][i].toLowerCase())){
     return true;
  } else {
    return false;
  }
}

}

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

When I had return true as part of the first code statement in the if conditional, the first test case in the exercise (['hello', 'hey']) wasn’t passing. Yet, when I moved it to outside of the for loop, the test case passed .

Why did moving return true to the outside of the for loop make it pass?

If I’m being super unclear, please let me know and I’ll elaborate further!

Your browser information:

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

Challenge: Mutations

Link to the challenge:

Code execution stops when the first return is encountered. I believe that you were returning before you checked the full array.

1 Like

Good question.

The thing about the return statement is that whenever your code encounters it in a function, it exits the function immediately, returning the value you provided. If you were in a loop, the rest of the loop will never execute.

Let’s look at your original code -

for(var i = 0; i < arr[1].length; i++){
  console.log(arr[1][i])
  if(arr[0].toLowerCase().includes(arr[1][i].toLowerCase())){
     return true;
  } else {
    return false;
  }
}

and let’s take mutation(["hello", "hey"]) as an example.

You begin looping through the string "hey", taking each letter in each iteration. But you have a return statement in both your if and else blocks. This means, either the if will execute or the else will execute, but irrespective of whichever does, your function will terminate immediately after, because you have a return statement in both cases. So essentially, your loop never moves beyond the first iteration.

In this particular case, your if block executes, because "h" from "hey" is present in the first string "hello", and your function terminates rightaway and returns true immediately. And this didn’t pass the first test case because you never checked the other two letters from "hey", and "y" isn’t present in "hello".

Now, moving the return true from the if block to outside the for loop ensures that you go through the whole string "Hey" first, and if any letter from "Hey" isn’t found in "Hello", then you can correctly return false.

But outside the loop, if you still haven’t returned at this point, then you know all letters from the second word were found within the first word, so you return true at this point, correctly.

Hope this helps.

2 Likes