Please explain Boolean and Arguments object in plain english

I’ve spent quite a bit of time on the challenges, Seek and Destroy and Falsy Bouncer.
It felt like there was huge jump in knowledge required compared with previous challenges, which had a good Freecode tone to them.

Trying to work out the solutions using the documentation is very difficult, and I was unable to do it. The Boolean and Arguments objects have various ways of being used and I would never have thought to use them in the way the solutions present.

Can anyone explain to a noob in plain language what these objects do and what their most common usage is?
Because even though I kind of understand the solution now, when I go back to read the documentation I find it is a bit abstract.

Your code so far

function destroyer(arr) {
  var allArgs = Array.prototype.slice.call(arguments);
 
  for (i = 0; i < arr.length; i++){
    for(j = 0; j < allArgs.length; j++){
     if (arr[i] === allArgs[j]) {
         delete arr[i];
         }
      }
    }
  
  
  return arr.filter(Boolean);

}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/seek-and-destroy

The arguments object is a special variable present for non-arrow functions (functions of the form function sum(a, b){ return a + b }). It collects all of the arguments that are passed to the function, so people use it when they don’t know ahead of time how many arguments will be passed to the function. It’s “array-like”, but you always have to do something like Array.prototype.slice.call(arguments) in order to create an array that you can use.

Boolean takes a value and returns a Boolean (true / false). 0, -0, null, false, NaN, undefined, or the empty string ("") get converted to false. Everything else gets converted to true.

That last line could be re-written as return arr.filter(function(e) {return Boolean(e)});, which might help in understanding what’s going on.

1 Like