Struggling for hours on Boolean and arguments

hey all!

so after struggling for a long while on these boolean questions i looked at the answer :frowning_face: after checking the code and reading the walk through of why i couldn’t figure out a few things. can anyone help me wrap my head around this. thanks in advance


function destroyer(arr) {
  //arguments?? its just a common variable? what is going on
  var realArr = Array.prototype.slice.call(arguments);
  //basic nested for loops, pretty casual
  for (var i = 0; i < arr.length; i++){
    for(var j = 0; j< realArr.length; j++){
      if( arr[i] === realArr[j]){
        //oh you can just call delete.... didnt know that
        delete arr[i];
      }
    }}
  //here is where i am totally lost. what just happened?
  //filtering Boolean false? and what happened to the other
  //two object arguments? they get filtered out below?
 return arr.filter(Boolean);
}

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


filter takes a call back function , in this case it is being given the Boolean object which takes an optional value as a parameter, the boolean object returns true for everything except : 0, -0, null, false, NaN

https://repl.it/@Dee3/SarcasticAmazingUnicorn

a=[true,9,"xyz",0, -0, null, false, NaN]
console.log(a) ///[true,9,"xyz",0, -0, null, false, NaN]
console.log(a.filter(Boolean)) ///[ true, 9, 'xyz' ]
1 Like

The filter is there to clean up the array after the potential deletion of members.

Just playing in the console (results are indented):

a=[1, 2, 3, 1, 2, 3];
    Array [ 1, 2, 3, 1, 2, 3 ]
delete a[4];
    true
a
    Array [ 1, 2, 3, 1, <1 empty slot>, 3 ]
a.filter(Boolean)
    Array [ 1, 2, 3, 1, 3 ]
1 Like

ok cool that makes sense. thank you guys.

but what about those other two parts of the array (think they would be considered objects?) where did they go and at what part of the process were they eliminated? in that first variable declaration?

The arguments object is available in all functions. It’s kinda like an array. You can access it with indices (arguments[0]) and you can get its length (arguments.length), but that’s it. When you want to turn it into an array, you can call Array.prototype.slice just like you see here. With the newer ES6 spec, you can just call Array.from, too.

However, that’s not why we’re calling Array.prototype.slice here. In this case, we’re calling slice in order to make a distinct copy of the arguments object. As far as I can tell, there’s not actually a good reason to do this, though.

Why do we need the arguments object at all? Take a look at how the function is called:

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

The first thing passed in an array. Everything after that is a number. There are three arguments in total. Now take a look at the function signature:

function destroyer(arr)

We only have one parameter - arr. That refers to the array we pass in, but how do we access the numbers that come after the array? Further, how do we do this in a way that lets us pass as many numbers as we want? This is why the arguments object is so useful. You can access anything that’s passed into an array, even if the function isn’t defined to take that thing as a parameter.

console.log(arguments); // [[1, 2, 3, 1, 2, 3], 2, 3]
2 Likes

You should note that the posted solution is not good though. The filtering would also remove any legitimate members that are false, eg. 0:

a=[1, 0, 3, 1, 2, 3];
   Array [ 1, 0, 3, 1, 2, 3 ]
a.filter(Boolean)
   Array [ 1, 3, 1, 2, 3 ]

There is no action being taken on the arguments other than the array that is being passed in, the contents of the argument that are not being passed into the function i.e. [2,3] are only used to compare values with the contents of the array that is being passed into the function, consequently the only action that is being taken is on the array that is being passed in.

you guys rock!

thank you! i think i kinda get it now. basically the second and third arguments are never passed through the function… ahhhhhhh ha!

really appreciate the help!

1 Like

You can call any js function with any number of arguments.
In top post, function ‘destroer’ explicitly names first passed argument as ‘arr’. If you want to access any additional arguments passed to the function, you should name them in function declaration or access them through special ‘arguments’ object.

You can also capture any additional arguments using spread operator (…)

function destroyer(firstArg, secondArg, ...allOtherArgs) { --

In this case spread operator captures these arguments into an array. allOtherArgs will be array.

ok good info! thank you! so it knows that i want to specifically pass the first argument to the function because it is the first in the list?

could i also pass only the second argument through the function? and how would i go about structuring that code? something like

function("", arg){
// something cool with code
}

thanks

there is no special way you can ignore some function arguments arguments like in other languages, but you can use ‘_’ (underscore) char for that purpose.
for example:

someArray.map((_, idx) => idx * 2)

You still can ‘_’-variable contents, but at least it’s clearer for future code reader that you just ignoring this value.