Question about the Mutations solution

Tell us what’s happening:
This seems like a silly question, but could someone please explain why the code below doesn’t solve the problem? The only differences from “solution 1” in the challenge guide is I swapped the positions of return true and return false and changed the < to >= within the if statement.

  **Your code so far**

function mutation(arr) {
let test = arr[1].toLowerCase();
let target = arr[0].toLowerCase();

for (let i = 0; i < test.length; i++) {
  if (target.indexOf(test[i]) >= 0) {
    return true;
  } 
}
return false;
}

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

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

Challenge: Mutations

Link to the challenge:

Have you tried following step-by-step one of the failing tests? What is each variable, what happens in the loop, does if condition is fulfilled, what happens if yes, what happens if not?

1 Like

Try to log out some of the values. See if that doesn’t help answer your question.

function mutation(arr) {
   let test = arr[1].toLowerCase();
   let target = arr[0].toLowerCase();

   for (let i = 0; i < test.length; i++) {
      if (target.indexOf(test[i]) >= 0) {
         console.log(target)
         console.log(test)
         console.log(test[i])
         return true;
      }
   }
   return false;
}
console.log(mutation(["hello", "hey"]))
1 Like