DEV Community

Randy Rivera
Randy Rivera

Posted on

Return True If The String In The First Element Of The Array Contains All Of The Letters Of The String In The Second Element

function mutation(arr) {
  return arr;
}

mutation(["hello", "hey"]);
Enter fullscreen mode Exit fullscreen mode
  • For example, ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring case.
  • The arguments ["hello", "hey"] should return false because the string hello does not contain a y.

Hint:

  • If everything is lowercase it will be easier to compare.
  • Our strings might be easier to work with if they were arrays of characters.
  • A loop might help. Use indexOf() to check if the letter of the second word is on the first.

  • Answer:

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

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

mutation(["hello", "hey"]); // will display false
Enter fullscreen mode Exit fullscreen mode

Top comments (0)