Failing five tests

Tell us what’s happening:

What’s wrong with this code? I have written the code and it fails the following tests:

I dont know why? Can anyone suggest something?

Your code so far


function mutation(arr) {

// Step 1: Loop through all the element of the first element of the array


for (let i = 0; i < arr[1].length; i++) {
  // console.log(arr[1].length);

  if (typeof(arr[0].toString().toLowerCase().indexOf(arr[1][i].toString().toLowerCase())) == 'number') {
    console.log(arr[0].toString().toLowerCase().indexOf(arr[1][i].toString().toLowerCase()))
    return true;
  }
  else {
    return false;
  }
}
 
// Step 2: Compare the second element to the first element using substring
}
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/80.0.3987.149 Safari/537.36.

Challenge: Mutations

Link to the challenge:

Can you please explain the logic you are using here?

Also, do you understand that you never make it passed i = 0? You return true or false in the first iteration before exiting the function.

There is no reason to use the toString method as both of arr's elements are already strings.

There is no situation where indexOf will not return a number, so your if statement will always evaluate to true in the first iteration (or any iteration, if you were not always returning a value).

Honestly, it is pure luck that you are passing the other tests. It just so happens, that all of the tests you pass expect a true value to be returned and since your function will only return true, you pass those specific tests.