0

There is 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.

Here is my code, but I am unable to solve the problem.

function destroyer(arr) {
// First I have converted the whole input into an array including the arguments
var args = Array.prototype.slice.call(arguments);
var abc=args.splice(0,1);//Here I have taken out the array part i.e[1,2,3,1,2,3] and also now args contain 2,3 only

function des(abc){
           for(var i=0;i<abc.length;i++){//I tried to check it for whole length of abc array
                if(args.indexOf(abc)===-1){
   return true;   //This should return elements of [1,2,3,1,2,3] which are not equal to 2,3 i.e [1,1] but for string inputs it is working correctly.How to do for these numbers?
     }
  }
}
return arr.filter(des); //but this filter function is returning  empty.How to solve my problem??
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);

For destroyer(["tree", "hamburger", 53], "tree", 53) the code is giving output ["hamburger"],which is working fine.

But for destroyer([1, 2, 3, 1, 2, 3], 2, 3); code is giving no output.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Uzma Khan
  • 139
  • 1
  • 3
  • 14
  • I already anwer this question about destroyer function but I can't find the answer now, check this http://stackoverflow.com/questions/33274362/javascript-remove-multiple-values-from-array-using-filter-and-loop – jcubic Jan 08 '16 at 12:13
  • 1
    Is this some kind of test or a quiz because here is another question about same function? http://stackoverflow.com/questions/33335130/how-to-remove-elements-from-an-array-using-arguments-object-whats-wrong-with – jcubic Jan 08 '16 at 12:18

5 Answers5

1

You can use Array.filter. Following example depicts the same

Also destroyer([1, 2, 3, 1, 2, 3], 2, 3); in this call, [1, 2, 3, 1, 2, 3] is first argument, 2 is second and 3 is third. So arr will have be [1, 2, 3, 1, 2, 3] and not [1, 2, 3, 1, 2, 3], 2, 3

function removeElementFromArray(arr,num){
  var _temp = arr.filter(function(item){
      return (item !== num);
  });
  
  return _temp;
}

(function main(){
  var arr = [1,1,1,2,4,3,2,4,5,4,3,1,4,5,2];
  var result = removeElementFromArray(arr,1);
  var result1 = removeElementFromArray(arr,3);
  
  console.log(result, result1);
})()
Rajesh
  • 24,354
  • 5
  • 48
  • 79
0

Lodash has without method that can be useful.

link to doc.

From lodash docs:

_.without(array, [values])

Creates an array excluding all provided values using SameValueZero for equality comparisons.

Example:

_.without([1, 2, 1, 3], 1, 2);
// → [3]
0

This should work for you:

function destroyer(arr) {
    var args = Array.prototype.slice.call(arguments);
    var abc = args.splice(0, 1);
    function des(value) {
        // you don't need loop here, just check if given value is in args array
        return args.indexOf(value) === -1;
    }
    // splice returns array of deleted elements so that your first argument is abc[0]
    return abc[0].filter(des);
}
var result = destroyer([1, 2, 3, 1, 2, 3], 2, 3);
console.log(result);
madox2
  • 49,493
  • 17
  • 99
  • 99
  • `function destroyer(arr) { // Remove all the values var args = Array.prototype.slice.call(arguments); var abc=args.splice(0,1); function des(abc){ return args.indexOf(abc)===-1; } return arr.filter(des); } destroyer([1, 2, 3, 1, 2, 3], 2, 3); ` – Uzma Khan Jan 08 '16 at 12:37
  • Thanks a lot for your help,I changed my code like this but I think abc[0] was not necessary arr would suffice.I got all test cases running.Thanks. – Uzma Khan Jan 08 '16 at 12:39
  • You are welcome! Just don't use `abc` as an argumet name of your filter function. – madox2 Jan 08 '16 at 12:42
0

Try this:

function destroyer(arr) {
    var args = Array.prototype.slice.call(arguments, 1);

    function des(abc){
        return args.indexOf(abc) === -1;
    }
    return arr.filter(des);
}
var result = destroyer([1, 2, 3, 1, 2, 3], 2, 3);
document.getElementById('output').innerHTML = JSON.stringify(result);
<pre id="output"></pre>
jcubic
  • 61,973
  • 54
  • 229
  • 402
0

Try this easy code:

function destroyer(arr) {
  /* Put all arguments in an array using spread operator and remove elements 
     starting from 1 */
  const args = [...arguments].splice(1);
  /* Check whether arguments include elements from an array and return all that 
     do not include(false) */
  return arr.filter(el => !args.includes(el));
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3); // [1, 1]
GoodGuyNick
  • 176
  • 4