Algorithm Seek and Destroy

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

🏁 Problem Explanation:

This problem is a bit tricky because you have to familiarize yourself with Arguments, as you will have to work with two or more but on the script you only see two. Many people hardcode this program for three arguments. You will remove any number from the first argument that is the same as any other other arguments.

Relevant Links

πŸ’¬ Hint: 1

You need to work with arguments as if it was a regular array. The best way is to convert it into one.

try to solve the problem now

πŸ’¬ Hint: 2

You need to filter, this also means you need to create a callback function, one that checks if the element is on the indexOf()

try to solve the problem now

πŸ’¬ Hint: 3

To convert arguments into an array use this code var args = Array.prototype.slice.call(arguments);

try to solve the problem now

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

πŸ”° Basic Code Solution:

function destroyer(arr) {
  var args = Array.prototype.slice.call(arguments);
  args.splice(0, 1);
  return arr.filter(function(element) {
    return args.indexOf(element) === -1;
  });
}

πŸš€ Run Code

Code Explanation:

  • The first line will turn the arguments variable into a full array instead of the limited array it currently is.
  • Next I remove the first argument since I don’t need, since I only want the other arguments passed besides the first which is the array we are going to compare against.
  • Then use the filter() to filter out the elements that are on the array and keep the ones that are not.

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 @Rafase282 for your help with Algorithm: Seek and Destroy

πŸ“‹ 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.