** Explanation Needed ** Mutations

Link to challenge:


function mutation(arr) {
  var firstWord = arr[0].toLowerCase();
  var secondWord = arr[1].toLowerCase();

  for (var i = 0; i < secondWord.length; i++) {
    if (firstWord.indexOf(secondWord[i]) === -1)
    return false
  return true;

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

My confusion is specifically on this line of code:

if (firstWord.indexOf(secondWord[i]) === -1)

I understand that secondWord[i] loops through each letter of hey. And the general function of indexOf is to return the position of the occurrence of the value. But what role does indexOf play here?

I have edited your post to include spoiler tags since this is a full working solution.

Are you confused on the -1 part?
If so , -1 is returned if it is not found.

For example, the letter "y" does not exist in the word "hello" so indexOf will return -1 and therefore your function would return false

1 Like

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