Created
April 13, 2022 21:02
-
-
Save jcolebot/00bd324e4a01196406a649e84b7c94a8 to your computer and use it in GitHub Desktop.
freeCodeCamp Seek and Destroy Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. | |
| // Remove all elements from the initial array that are of the same value as these arguments. | |
| // Note: You have to use the arguments object. | |
| function destroyer(arr, ...args) { | |
| return arr.filter(elem => !args.includes(elem)) | |
| } | |
| destroyer([1, 2, 3, 1, 2, 3], 2, 3); | |
| // Nested Loop Solution | |
| function destroyer(arr) { | |
| let valsToRemove = Object.values(arguments).slice(1); | |
| for (let i = 0; i < arr.length; i++) { | |
| for (let j = 0; j < valsToRemove.length; j++) { | |
| if (arr[i] === valsToRemove[j]) { | |
| delete arr[i]; | |
| } | |
| } | |
| } | |
| return arr.filter(item => item !== null); | |
| } | |
| // Arguments Array Conversion | |
| function destroyer(arr) { | |
| var valsToRemove = Array.from(arguments).slice(1); | |
| return arr.filter(function(val) { | |
| return !valsToRemove.includes(val); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment