Seek and Destroy solution questions

Hi!
I’ve been stuck at Seek and Destroy problem for too long, so I checked the solution. I know, it’s not a good thing in learning process.

What exactly happens when Array.prototype.slice.call(arguments) is being used inside of a function? Or

  • arr.filter(Boolean);
  • Array.from(arguments).slice(1);
    And where would I learn those things in more detail? Because I have to say, FCC resources are not enough for me. I suppose I should’ve learned about Boolean object in some of previous challenges, but it’s like taking it to completely different level.
    I have some background in Java, and gotta admit, things like Array.prototype.slice.call(arguments), are in no way object oriented, MDN didn’t help me at all, and StackExchange just confused me with some things I have never seen.
    It just feels like I am expected to be comfortable with OO, where I’m completely not, the only solution I came up with on my own was this pre-1970s long procedure with "if"s, "for"s, temporary array(s) etc.

If it helps, think of Array.prototype.slice as a static method. We have to reference the function there because the arguments object isn’t actually an array - if you tried arguments.slice(), you’d get an error. So, to call slice on it, we have to pass the arguments object to call, which is just the same as arguments.slice(). The reason you want to do this is because slice returns an array which lets you operate on the function’s arguments with array methods and loops.

Array.from(arguments).slice(1)

Array.from is a newer method that does the same thing as above, but in a more readable way. slice is used because the first argument to the function is the array being changed and all we want is an array of the objects to destroy.

arr.filter(Boolean)

Boolean (with a capital B) is a constructor method. Any expression you pass in will be evaluated and the return value is a boolean representing the expression’s truthiness.

var a = Boolean(true); // true
var b = Boolean(false); // false
var c = Boolean(''); // false (empty strings are falsey)
var d = Boolean("string"); // true (non-empty strings are truthy)

In your example, the function is passed to the filter method. This means the Boolean function will be used on each item in the array. Passing functions in this way is identical to method references in Java 8.

These things all make sense eventually. Keep reading the MDN when you get stuck. Even if you don’t understand it right away, it’s a good habit to get into, and someday you’ll find that you’re able to read it just fine.

4 Likes

Hey I just finished this challenge myself, it was VERY challenging, I had the same feeling about MDN as well. I dont understand most of the stuff theyre talking about, esp if the examples are different than my problem. I watched aome youtube videos to help me understand.

and

were used to change the Arguments to an array. supposedly you cant do certain things with object arguments.

the

is to eliminate the falsy values from the arr (since they used “delete”) the positions of the deleted values would be “null”.

Hope it helps.

1 Like

You’re writing an algorithm in the form of a function; the OO aspects of JS are basically implementation details here, they’re not massively relevant to how to actually make the function work (see below tho).

  • filter is a method (a function) that is available to arrays.
  • it takes a function as an argument. Functions are values in JS, same as anything else.
  • Boolean(someValueHere) is a function that converts/coerces a value to a Boolean.
  • arr.filter(Boolean) would be an example of reduction: it is the same as arr.filter(function (val) { return Boolean(val) });. Because the argument is a function that passes it’s single argument to another function that takes that same single argument, you can simplify it to arr.filter(Boolean);. It’s algebra, it’s not anything clever, it makes the code terser but a bit more difficult to read if you don’t understand what’s going on.

JS is not classical OO like Java, it’s prototypical OO. JS is the only mainstream language that works based on this form of OO, but it is very much OO. Everything is either an object, or is boxed into an object to manipulate it (eg strings are not objects, but running 'hello'.toUpperCase() will box the string in the String object, do the manipulation, and give you back a new string).

What is means in practice is that (dynamic) methods for objects are defined on another object, called a prototype, which is shared between all instances. When you construct an object, you get all that for free.

Furthermore this is how you get inheritance in JS: at the top there is the basic Object, which has an Object.prototype with some methods. Other objects inherit from this, like Array. Array has its own prototype, with methods unique to arrays like filter, but it also gets access to all the methods from Object.prototype as well (eg toString, instanceOf).

  • arguments is an object that is a bit like an array, but it isn’t an array, so does not have access to the methods in Array.prototype.
  • arguments is available inside the scope of every function and is a record of the arguments given to that function. It is one of the more common uses of reflection in JS.
  • call is a method defined on Function.prototype. It lets you call a function in a different context. The first argument is that context, then the rest of the normal arguments go after. So Array.prototype.slice.call(arguments, 0) is saying call the method slice that is on the Array.prototype object, but use it on arguments. It is borrowing the slice method from Array.prototype and applying it to arguments instead of a normal array.