DEV Community

Hasib Muhammad
Hasib Muhammad

Posted on

2 1

A basic use case of Arguments or Rest Parameters

Seek and Destroy

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.

destroyer([1, 2, 3, 1, 2, 3], 2, 3) should return [1, 1].
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3) should return [1, 5, 1].
destroyer([3, 5, 1, 2, 2], 2, 3, 5) should return [1].
destroyer([2, 3, 2, 3], 2, 3) should return [].

Approach:

  • Make an array with with the parameters except the 1st array
  • Filter the 1st array excluding the new array items

Using Arguments:

function destroyer(arr) {
    let newAr = [];
    for( let i = 1; i < arguments.length; i++ ) {
      newAr.push( arguments[i] );
    }
    return arr.filter( item => !newAr.includes(item) );
}
Enter fullscreen mode Exit fullscreen mode

Using Rest Parameters:

const destroyer = (...arr) => {
    const checkedArr = [...arr][0];

    let newAr = [];
    for( let i = 1; i < [...arr].length; i++ ) {
      newAr.push( [...arr][i] );
    }

    return checkedArr.filter( item => !newAr.includes(item) );
}
Enter fullscreen mode Exit fullscreen mode
👋 Was this post helpful?

Please leave your appreciation by commenting on this post!

It takes one minute and is worth it for your career.

Join now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay