0

I'm sure this is extremely simple but I can't find a way to do it.

function([5, 4, 6,],1, 2, 3);

There's an array, and then there are things outside of the array, but I don't know how are these outside things called or manipulated?

So how do i select all the elements outside of the first array?

Thanks and sorry for a dumb question.

Edit, here's the whole function because i can't seem to explain it well.

function destroyer(arr) {
  // Remove all the values
  return arr;
}

destroyer([1, 2, "asd", 1, 2, 3], "kk", 3);

EDIT2; here is the solution to the problem of selecting arguments:

The arguments object is not an Array. It is similar to an Array, but does not have any Array properties except length. For example, it does not have the pop method. However it can be converted to a real Array:

var args = Array.prototype.slice.call(arguments,1)

This selects everything besides the first argument.

Treborx555
  • 3
  • 1
  • 3
  • Please share what should be the output of this function call and what you intend to do with these arguments. – gurvinder372 Jul 14 '16 at 11:00
  • I have to select the numbers outside of the array to use in a filter function to filter the array, but i need to know how to select the numbers first to do that. – Treborx555 Jul 14 '16 at 11:02
  • you have here function with for arguments, first is array the three others are number, so just use second, third and forth arguments of your function – Ruben Karapetyan Jul 14 '16 at 11:04
  • I don't know how to do that, that's why i'm asking. If i use arr[0] it selects the first element inside the array instead of the whole array.. That's why i'm confused. – Treborx555 Jul 14 '16 at 11:16
  • Possible duplicate of [is it possible to get all arguments of a function as single object inside that function?](http://stackoverflow.com/questions/4633125/is-it-possible-to-get-all-arguments-of-a-function-as-single-object-inside-that-f) – Rajesh Jul 14 '16 at 11:23
  • Thank you, i was able to access the things outside of the array with this! – Treborx555 Jul 14 '16 at 11:33

2 Answers2

1

EDIT:

Use arguments keyword inside your function

function destroyer(arr) {
  // Remove all the values
  arr = arguments
  return arr;
}

destroyer([1, 2, "asd", 1, 2, 3], "kk", 3);
Arif
  • 1,617
  • 9
  • 18
  • The exercise i'm given put only a single argument in the function like function(arr), from that i have to select elements outside of the array. Like this: function destroyer(arr) { //select things outside of array } destroyer([4,5,6]1, 2, 3); – Treborx555 Jul 14 '16 at 11:06
  • I updated my main post with the original code for the challenge and changed the number to string to remove confusion. I'm sorry i didn't think i was being unclear. – Treborx555 Jul 14 '16 at 11:19
  • You can not do that without extending the parameters of that function accepts. Or you should call `destroyer` with one object which will contain all the other parameters – Arif Jul 14 '16 at 11:23
  • This would be the first challenge then where i'd have to change anything inside function(arg) which makes me think i shouldn't be doing that. I just tried returning arg and it only returns the array, not the numbers.Maybe that means you're right. – Treborx555 Jul 14 '16 at 11:26
  • you can keep `function destroyer(arr) { return arr; }` But change `destroyer(arr)` to `destroyer(obj)` – Arif Jul 14 '16 at 11:28
  • Please recheck my answer. I've almost forgot about arguments keyword :)) Hope it helps – Arif Jul 14 '16 at 11:32
  • Thank you, argument does the work, i'll have to read up more on it now :3 – Treborx555 Jul 14 '16 at 11:34
0

You can do it this way

//suppose you have a function like this
function f1()
{
   var args = [].slice.call(arguments);
   //console.log(args);
   var arr = args[0];
   args.splice(0,1);
   return arr.filter( function(item){
      return args.indexOf( item ) != -1;
   });
}

//since no items outside array are matching, it will return empty
console.log(f1( [5, 4, 6,],1, 2, 3 ));
                 
//since two items outside array are matching, it will filtered array
console.log(f1( [1, 2, 5, 4, 6,],1, 2, 3 ));
                 
                 
gurvinder372
  • 66,980
  • 10
  • 72
  • 94