0

Working on a challenge from freeCodeCamp, where I have to create a function that takes in an array and a number of other arguments. If an element within the passed array matches one of the passed arguments, it has to be removed from the array.

I have written this code, but it does not work for all of the tests:

function destroyer(arr) {

  var args = Array.prototype.slice.call(arguments);

  for(i=0; i<arr.length; i++){
    for(var j=0; j<args.length; j++){
      if(args[j]===arr[i]){
        arr.splice(i,1); 
      }
    }
  }  
  return arr;
}

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

Step 1: Convert arguments object into an array, so I can treat it like one. Step 2: Run nested for-loop to find any matches between the passed array and the passed array of arguments. Step 3: If a match is found, eliminate it using .splice(i,1).

Shouldn't that do the trick? What am I doing wrong?

EDIT: This is an example of a test that it fails:

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

1
  • maybe give an example of a failing test ? Commented Jun 29, 2017 at 7:42

2 Answers 2

4

You are changing array while iterating over it. So after the first splice indices change.

You could use filter and indexOf to do this.

function destroyer(array) {
  var toRemove = [].slice.call(arguments, 1);
  
  return array.filter(function(item) {
    return toRemove.indexOf(item) === -1
  })
}

console.log(destroyer([3, 5, 1, 2, 2], 2, 3, 5))

If you still want to use for-loop. But keep in mind that splice mutates array. So your function does mutate arguments which should usually be avoided.

function destroyer(arr) {

  var args = Array.prototype.slice.call(arguments);

  for(var i=0; i<arr.length; i++){
    for(var j=1; j<args.length; j++){
      if(args[j]===arr[i]){
        arr.splice(i,1);
        i--; // manually reduce index
      }
    }
  }  
  return arr;
}

console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));
console.log(destroyer([3, 5, 1, 2, 2], 2, 3, 5));

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that every time you remove an item from arr, your loop will skip the next item. Imagine you're iterating over the following array:

['a', 'b', 'c', 'd', 'e']

Say you're at iteration 2, corresponding to the 'c' item in the array and you remove that item. Your array will now look like this:

['a', 'b', 'd', 'e']

Now 'd' will correspond to the current iteration 2 but your for lopp will continue on with the next iteration, 3, and the 'd' item will never be processed.

Since the array is being modified, I would suggest using a while loop instead:

var i = 0;
while (i < arr.length){
  for(var j=0; j<args.length; j++){
    if(args[j]===arr[i]){
      arr.splice(i,1); 
    } else {
      i++;
    }
  }
}

Here, i is not incremented if you removed the item since that meant the next item was moved to the current position of i

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.