Where is the issue in this code?

The result should be an array with these values [1, 3, 1, 3]

function destroyer(arr) {
  function ffs(a, b) {
    for(let i = 0; i < a.length; i++) {
      if(b === a[i]) {
        return a.splice(i, 1)
      }
    }
  }
  return arr;
}

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

One is you pass in 2 arguments here:

to a function with 1 parameter:

and another is this function is never called:

so what is returned is the first argument you pass to the destroyer function.

If you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.


Note: You have to use the arguments object.

The note is hinting at what you have to do. I should say you can use other methods like rest parameter syntax instead of the arguments object.

Challenge:

function destroyer(arr, b) {
for(let i = 0; i < arr.length; i++) {
if(b === arr[i]) {
return arr.splice(i, 1)
}
}
return arr
}

What about this? It’s returning [2] instead of cutting it out of the array

Look at the function calls in test section.

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] .

It is a variadic function. Meaning you get a different amount of arguments passed to the function so you can’t hardcode the parameter list (Well, technically I’m guessing you might be able to pass the challenge using a hardcoded parameter list, but that is beside the point)

The point is your function should not expect a fixed amount of arguments. All you can be certain of is the first argument is always an array.

2 Likes

Hey!
As @lasjorg was explaining you want to use rest parameters here.
For example,

const destroyer = (array, ...args) {

}

args will be an array that contains additional parameters.
So for a function call destroyer([1, 2, 3, 1, 2, 3], 2, 3), your function will get 2 parameters:
array = [1, 2, 3, 1, 2, 3]
args = [2, 3]
The expected output is an array that does not contain any of the items from args array.

  1. You want to remove the args items from input array
  2. You need to return the result as an array

I’d personally use .filter (Array.prototype.filter() - JavaScript | MDN) and .includes(Array.prototype.includes() - JavaScript | MDN) methods here to filter out the items that args array does not contain. So make sure to use ! syntax :wink:

But you can also filter the array with a for loop.

You can read up on splice here:

"Return value

An array containing the deleted elements.

If only one element is removed, an array of one element is returned.

If no elements are removed, an empty array is returned."

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