DEV Community

Mohammed Awad
Mohammed Awad

Posted on

Seek and Destroy Algorithm

DESCRIPTION:

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.
hint : use arguments object

arguments object from W3schools

JavaScript functions have a built-in object called the arguments object.
The argument object contains an array of the arguments used when the function was called (invoked).
This way you can simply use a function to find (for instance) the highest value in a list of numbers:

Examples

destroyer(["tree", "hamburger", 53], "tree", 53)
// should return ["hamburger"].

destroyer([2, 3, 2, 3], 2, 3) 
// should return [].
Enter fullscreen mode Exit fullscreen mode

My approach for solving this problem:

1- first read about arguments object (I think it's Ok to use google and learn new Thing I'm not an expert).

2- I need to sprite the arguments into 2 arrays,
one for parameter Arr and other for the rest Arguments.

3- filter the Arr and return item if it's not includes into OtherArguments.

My solution:

function destroyer(arr) {
  let OtherArguments = Object.values(arguments).splice(1)

  return arr.filter(item=> !OtherArguments.includes(item))
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);
Enter fullscreen mode Exit fullscreen mode

Other approach for solving this problem:

1- you don't need arguments object.

2- I need to sprite the arguments into 2 arrays,
one for parameter Arr and other for the rest Arguments.

3- we can use (...) rest parameter insted of Object.values.

4- filter the Arr and return item if it's not includes into OtherArguments.

Other solution:

function destroyer(arr,...OtherArguments) {

    return arr.filter(item=> !OtherArguments.includes(item))

}
destroyer([1, 2, 3, 1, 2, 3], 2, 3)
Enter fullscreen mode Exit fullscreen mode

Any tips or edit are most welcome. share it with me on the comments. Thanks for being here!

Follow Muhmmad Awd on

If you have any questions or feedback, please feel free to contact me at

Top comments (5)

Collapse
 
alexmustiere profile image
Alex Mustiere

I'd say :

const destroyer = (base, ...toRemove) => base.filter((item) => ! toRemove.includes(item));
Enter fullscreen mode Exit fullscreen mode
Collapse
 
xmohammedawad profile image
Mohammed Awad

very clever! Wow

Collapse
 
xmohammedawad profile image
Mohammed Awad

I included your answer.

Collapse
 
_eduard26 profile image
Eduard Constantin

I love one liners

Collapse
 
xmohammedawad profile image
Mohammed Awad

Any tips or edit are most welcome. share it with me on the comments. Thanks for being here!