Algorithm Mutations

🚩 Remember to use Read-Search-Ask if you get stuck. Try to pair program πŸ‘₯ and write your own code πŸ“

🏁 Problem Explanation:

  • 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…

Relevant Links

πŸ’¬ Hint: 1

  • If everything is lowercase it will be easier to compare.

try to solve the problem now

πŸ’¬ Hint: 2

  • Our strings might be easier to work with if they were arrays of characters.

try to solve the problem now

πŸ’¬ Hint: 3

  • A loop might help. Use indexOf() to check if the letter of the second word is on the first.

try to solve the problem now

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

πŸ”° Basic Code Solution:

Procedural

function mutation(arr) {
  var test = arr[1].toLowerCase();
  var target = arr[0].toLowerCase();
  for (i=0;i<test.length;i++) {
    if (target.indexOf(test[i]) < 0)
      return false;
  }
  return true;
 }

πŸš€ Run Code

Code Explanation:

First we make the two strings in the array lowercase. test will hold what we are looking for in target.
Then we loop through our test characters and if any of them is not found we return false.

If they are all found, the loop will finish without returning anything and we get to return true.

Relevant Links

🌻 Intermediate Code Solution:

Declarative

function mutation(arr) {
  return arr[1].toLowerCase()
    .split('')
    .every(function(letter) {
      return arr[0].toLowerCase()
        .indexOf(letter) != -1;
    });
}

πŸš€ Run Code

Code Explanation:

Grab the second string, lowercase and turn it into an array; then make sure every one of its letters is a part of the lowercased first string.

Every will basically give you letter by letter to compare, which we do by using indexOf on the first string. indexOf will give you -1 if the current letter is missing. We check that not to be the case, for if this happens even once every will be false.

Relevant Links

πŸ† Credits:

If you found this page useful, you may say thanks to the contributors by copying and pasting the following line in the main chat:

Thanks @sxmxc @Rafase282 @anuragaryan @hallaathrad for your help with Algorithm: Mutations

πŸ“‹ NOTES FOR CONTRIBUTIONS:

  • ⚠ DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
  • Add an explanation of your solution.
  • Categorize the solution in one of the following categories β€” Basic, Intermediate and Advanced. πŸš₯
  • Please add your username only if you have added any relevant main contents. (⚠ DO NOT remove any existing usernames)

See πŸ‘‰ Wiki Challenge Solution Template for reference.