Algorithm Seek And Destroy - ashish9342/FreeCodeCamp GitHub Wiki
🚩 Remember to use Read-Search-Ask
if you get stuck. Try to pair program 👥 and write your own code 📝
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.
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
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
To convert arguments
into an array use this code var args = Array.prototype.slice.call(arguments);
try to solve the problem now
Solution ahead!
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
- 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.
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
⚠️ 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. (:warning: DO NOT remove any existing usernames)
See 👉
Wiki Challenge Solution Template
for reference.