0

"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." For some reason I can pass every other checkpoint except (["hello", "hey"]). any tips?

function mutation(arr) { 
  var firstIndex = arr[0].toLowerCase(); 
  var secondIndex = arr[1].toLowerCase();

  for(var i = 0; i < arr.length; i++) { 
     if(firstIndex.indexOf(secondIndex.charAt(i)) !== -1) { 
      return true; 
    } 
    else { 
      return false;
    }
  }
  return arr;
}

mutation(["hello", "hey"]);
Luis Ulua
  • 13
  • 3
  • Possible duplicate of [How to compare arrays in JavaScript?](http://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – Amitesh Kumar Sep 26 '16 at 05:05

1 Answers1

0

The idea is to check every character in secondIndex to see if it is contained in firstIndex. So the logic here would be: for ever character in secondIndex, if the character is not in firstIndex, return false (ending the function). If the function doesn't end after checking all the characters, you know that each character was found in firstIndex, and you can then return true.

function mutation(arr) { 
  var firstIndex = arr[0].toLowerCase(); 
  var secondIndex = arr[1].toLowerCase();

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

mutation(["hello", "hey"])
Howzieky
  • 829
  • 7
  • 18
  • Haha there were a couple things that needed to be fixed, such as the fact that you were looping through `arr` instead of the characters in `secondIndex`. Also, your logic was saying, "loop through each character, and if we find one that matches, return true. But if we find one that doesn't match first, return false." Good luck with your project! – Howzieky Sep 26 '16 at 01:31