Mutations - dont know how to compare words

function mutation(arr) { 

for(var i = 0; i < arr.length; i ++) { 
  arr[i] = arr[i].toLowerCase().split('');  
  if (arr[0].indexOf(arr[0]) == arr[0].indexOf(arr[1])) {  
    return true;
  } else {
    return false;
  }  
}
  return arr;
}

mutation(["Mary", "Army"]);

any idea on how can i compare these two word s/ or what am i doing wrong? thank you

I get true when I run the following with your code.

mutation(["Mary", "Army"]);

Your code is correctly returning true, because all of the letters in the second argument “Army” are in the first argument “Mary”.

Which test(s) are you failing?

Im failing every tests that are supposed to show “false”

Actually, I figured it out. Your algorithm is going the wrong direction.

First of all, not sure why you are looping through the array when you know it will always have only 2 items. You can refer to those as arr[0] and arr[1]. Once you realize that, you only need to create a loop which loops through each letter of arr[1] and see if that letter is somewhere in arr[0]. You are on the right track using indexOf, but you only need to be using it on arr[0] and not on both.

Whenever you are unsure what a specific line of code is doing, console.log() is your friend. I was going to try to explain why your code shows true for everything, but it was complicated and I felt it was better to point a few mistakes out and get you started in the right direction. If you need more help or hints, please feel free to ask.

Thank you for your reply. My main reason for loop was that i wanted to lowercase all words as just using .toLowerCase(). wasnt working for me .

You will still have a for…loop but you don’t have to loop through an array. Also, I find it easier to work with variables names instead of things like arr[0] or arr[1]. If you want to convert the words to lower case you must assign them to a new variable because strings themselves are immutable.

For example, it might help to do something like this at the beginning of your function:

  var lettersToCheck = arr[0].toLowerCase();
  var lettersToFind = arr[1].toLowerCase();

Now you have lower case versions of each word. The best part is, you no longer have to refer to arr[0] or arr[1] or remember which one is supposed to be checking which, because your variable names make it clear.

1 Like

ok got it … thank you :slight_smile:

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

function mutation(arr) {
  
  var lettersToCheck = arr[0].toLowerCase();
  var lettersToFind = arr[1].toLowerCase();

for (var i = 0; i < lettersToFind.length; i ++) {
  

  
  if (lettersToCheck.indexOf(lettersToFind[i]) >= 0) {
    
        return true;
      } else {
        return false;
      } 
} 
  return arr;
}

mutation(["hello", "hey"]);

it doesnt return false in case [“hello”, “hey”] but other variants passes … any hint about what im doing wrong ?

Your code checks only the first letter and after that it returns.

You’re trying to see if each index of arr[1] matches an index of arr[0]. Hint: nested loops, one for each item.

I like your idea of lower-casing both items. I used RegExp to ignore case in my solution.