Seek and Destroy Algorithm

xmohammedawad

Mohammed Awad

Posted on May 12, 2023

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

💖 💪 🙅 🚩
xmohammedawad
Mohammed Awad

Posted on May 12, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Seek and Destroy Algorithm
javascript Seek and Destroy Algorithm

May 12, 2023

FizzBuzz for Javascript
javascript FizzBuzz for Javascript

July 10, 2021