Challenge: Mutations, broken tests?

Tell us what’s happening:
Why does my code not pass the tests, if the cases that it shows me wrong, it does pass them in my browser?

  **Your code so far**

let result=true
function mutation(arr) {
for (let i = 0; i < arr[1].length; i++) {
  if (!arr[0].toLowerCase().includes(arr[1][i].toLowerCase())) {
    result = false;
    return result;
  }
}
return result
}

mutation(["hello", "hey"]);
  **Your browser information:**

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

Challenge: Mutations

Link to the challenge:

Big hint: Do not use global variables in your function. There is no reason that result should be a global declared outside of the function body. This is bad form and a cause of bugs, such as in your case.

2 Likes

Thanks for your answer. Certainly, if I declare the result variable inside the function, it passes the tests without problem, although I don’t quite understand why it doesn’t work as a global variable in my initial solution.

I think it is important that you understand why it isn’t working.

Answer me this, after the first time the if condition is false and result is set to false, where in your code is it possible for result to ever be set back to true?

2 Likes

Ok, now I get it, thank you very much. If result is false as a global variable, then you cannot pass the next test again because if you have to return true as a result, there is nowhere to change it.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.