Can someone explain a line of code in intermediate-algorithm-scripting 3rd challenge("Seek and destroy")

Tell us what’s happening:
Describe your issue in detail here.

I dont really understand this line of code:
let valsToRemove = Object.values(arguments).slice(1);
why cant we just use arr.slice(1);

  **Your code so far**

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);
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36

Challenge: Seek and Destroy

Link to the challenge:

@Ranko50 The arr is only for the first argument.

In this case, it is the [1, 2, 3, 1, 2, 3] array. The other arguments 2, 3 are not included.

The Object.values(arguments).slice(1) took out those 2 arguments which are
not included in arr.

The Object.values function returns the values of the object which is passed in as array.

In this case, arguments is an object like this {"0": [1, 2, 3, 1, 2, 3], "1": 2, "2": 3}

So when the arguments is passed into the Object.values function, the function returns [[1, 2, 3, 1, 2, 3], 2, 3]. And the .slice(1) method slice out the element(s) starts from index 1 to end.

So the valsToRemove variable is then assigned to [2, 3].

If we use arr.slice(1) the valsToRemove will be assigned to [2, 3, 1, 2, 3], which will make the algorithm not work anymore.

Hope This Helps

3 Likes

Thank you, it helps a lot . I was confused about that line of code because I thought I could retrieve arguments 2,3 with arr[1] :smiley:

the function has parameter arr, but in fact, we only expect the first element to be an array and other additional parameters to be included. Since we do not know what other parameters we will get, we use the arguments object, which functions have, which is an object which contains all arguments the function receives as key/value pairs. We use the Object.values(object_name) method to get an array of of all values inside the respective object, which is the arguments object. Then, thru array.slice() method we take all values(all parameters of the function) excluding the fist parameter which is arr(the array). If we were to use arr.slice() we execute slice on the first function argument, the array, we dont access the other arguments.

function destroyer(arr) {
  console.log(arr)  //[ 1, 2, 3, 1, 2, 3 ]
  console.log(arguments)  //{ '0': [ 1, 2, 3, 1, 2, 3 ], '1': 2, '2': 3 }
  console.log(Object.values(arguments)) //[ [ 1, 2, 3, 1, 2, 3 ], 2, 3 ]

  let valsToRemove = Object.values(arguments).slice(1);
  console.log(valsToRemove) //[ 2, 3 ]
  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);
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);
1 Like

@Ranko50 The arr is the limited. When you put a , after every object that first object became the arr. For example:

  • If you called the function like this destroyer(1, 2, 3) the arr is the first argument which come before first comma.
  • If you call the function like this destroyer([1], 2, 3) the arr is the Array [1].

So if you add another argument, let’s say arg, and if you call the function like this: destroyer(1, 2, 3), the arr will be 1 and arg will be 2.

The main point is that, the first parameter that you set to pass in will only be the first argument which come before the first comma. And like so, second will be the second one and third one will be third one but not every arguments passed in.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.