Implement the Mutations Algorithm - Implement the Mutations Algorithm

Tell us what’s happening:

Hi everyone (sorry for my bad english), i pass the lab with this code but i want to know how use the return statement in a loop. I create a variable call “boolean” and assign “false” if the loop beak and “true” when the loop don’t break.
I use this way because i don’t know where to put return true or return false inside the loop. The loop stop at the first return statement.

Your code so far

function mutation(array) {
let str1 = array[0].toLowerCase();
let str2 = array[1].toLowerCase();
let boolean;

  for (let char of str2) {
    if (str1.includes(char) === false) {
      boolean = false;
      break;       
    }
    boolean = true;
  }

return boolean;
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36

Challenge Information:

Implement the Mutations Algorithm - Implement the Mutations Algorithm

you can directly return “true” or “false” from your loop, but int his case its better to break out of it and then returning it, happy coding :slight_smile:

1 Like

Where did you put the return statements in the loop before? Consider when it’s okay for this loop to exit with the return, and when it should keep going.

1 Like

Without the boolean variable, you would return false where you’re currently setting boolean to false. No need to break or set boolean to true. If it goes through the loop without returning, it will hit your final return statement, which would be true, right?

1 Like

Hi, remove ”let boolean;” and “return boolean;”. Then replace "boolean = false” by “return false” and “boolean return = true” . My return statement was inside the loop.

Hi you right i set boolean to true (let boolean = true;), then i remove “boolean = true” and “break;” and it work.

  for (let char of str2) {
    if (str1.includes(char) === false) {
     return  false;
    }
  }

return true;

You don’t need a boolean variable.

Note that the critical thing here is the location of the return statement for ‘true’