7.0 KiB
title |
---|
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. You can use various methods like: indexOf()
, includes()
. If you need another approach, reduce()
might also be of use; keep reading those docs!
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!
Solution ahead!
Basic Code Solution:
function destroyer(arr) {
var args = Array.prototype.slice.call(arguments);
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < args.length; j++) {
if (arr[i] === args[j]) {
delete arr[i];
}
}
}
return arr.filter(Boolean);
}
Code Explanation:
-
Create an array of
arguments
usingArray.prototype.slice.call()
and store it in the variableargs
. We'll use this to check againstarr
. -
Start a basic
for
loop to iterate througharr
. Nest anotherfor
loop inside the first, changing the integer variablej
and arr to args. This second loop will iterate throughargs
.-
Within the second loop create an
if
statement, checking strictly===
that the current val ofarr[i]
is equal toargs[j]
. -
If the value at the current index is equal in both arrays, use
delete
to remove it fromarr
.
-
-
Outside of the nested loops: return the modified array using the
Boolean
object as a filter for anynull
's created by thedelete
operator.
Relevant Links
- [arguments
- Array.filter()
- delete
- Boolean
Intermediate Code Solution:
function destroyer(arr) {
var args = Array.from(arguments).slice(1);
return arr.filter(function(val) {
return !args.includes(val);
});
}
Code Explanation:
-
Declare a variable named
args
and set it equal to a newArray
objectfrom()
thearguments
passed into the function. On the same or next line, use theslice()
method onargs
starting from the second index, 1. This separates the arguments used for filtering into their own array ofargs
. -
Return the filtered array, using
includes()
in the callback function to check ifval
is not inargs
; returningtrue
to keep the value in the original array orfalse
to remove it.
Relevant Links
Advanced Code Solution:
const destroyer = (arr, ...args) => arr.filter(i => !args.includes(i));
Code Explanation:
- Code using ES6 syntax to declare function using arrow functions.
- Using spread operator to retrieve the arguments.
- Return the filtered array, using
includes()
.
Relevant Links
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.