Mutations Challenge. Why is my code not working?

function mutation(arr) {
  var firstWord = arr[0].toLowerCase().split("").sort();
  var secondWord = arr[1].toLowerCase().split("").sort();
  
for (var i = 0; i < arr[1].length; i++) {
    if (arr[0].indexOf(arr[1][i] < 0)) {
      return false;
    }

  }
     return true
}

console.log(mutation(["floor", "for"]));

Hello, I have ran through this code multiple times and it keeps returning false even if it shouldn’t in the if section. If I manually put in something like:
arr[0].indexOf(arr[1][0])
it will equal zero, and if I manually iterate through all of the numbers, 0, 1, 2 etc they all return numbers 0 or higher. Yet in the function it returns false when it shouldn’t. What am I doing wrong?

I’m unsure what you’re trying to do, is this a fCC challenge?

Anyway, so the first issue I have is that you define variables for firstWord and secondWord, but you never use them, your code only uses arr[0] and arr[1]. Not sure what those variables are for.

Second, and that’s your main problem, you used a return statement within the loop. As soon as that loop hits a case where the if-clause is true, the function will return false (which will terminate the function execution, so the loop will stop).

1 Like

Hey @The7O2Guy!

For future posts, it would be helpful if you include the challenge link so others in the forum can see what challenge you are referring to.

I will include it in my post here.

My first question is, Why did you create these variables if you are not going to use them?

Also, you don’t need to sort the strings.

FCC instructions:
Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.

Yikes, thats embarrassing haha. I knew I was overlooking something dumb. Thanks for the replies, and in the future I will provide the link

Also, the sort function was my part of my furst attempt before realizing it didn’t meet all the standards and I didn’t take it out.

2 Likes