Seek and Destroy challenge - Working solution, but I'm unsure if it's correct

Seek and Destroy challenge - Working solution, but I'm unsure if it's correct
0

#1

Hi all,

I finished the Seek and Destroy challenge (eg. passed the tests), but I don’t feel great about my solution. Something about it feels clunky and bloated.

Can you see anything obvious that would improve my code?

    function remover(arr, toRemove) {
        var removed = arr.filter(function(val){
            if (val == toRemove) {
                return false;
            } else {
                return true;
            }
        });
        return removed;
    }
    
    function destroyer(arr) {
        cleanArr = arguments[0];
        console.log(cleanArr);
        for (var i = 1; i < arguments.length; i++) {
            cleanArr = remover(cleanArr, arguments[i]);
        }
        console.log(cleanArr);
        return cleanArr;
    }

Thanks :slight_smile:


#2

Well one thing that I can immediately see, which has nothing to do with functionality of code, is

    function remover(arr, toRemove) {
        return arr.filter(function(val){
            return val !== toRemove
        });
    }

With that regard you may also consider removing remover() but it’s up to you.

Here’s mine if you are interested in.

function destroyer(arr) {
  var res = [];
  var blackList = [].slice.call(arguments, 1); // arguments excluding first argument.
  
  arr.forEach(function(elem) {
    if (blackList.indexOf(elem) == -1)
      res.push(elem);
  });
  
  return res;
}