0

I have been doing this course for hours on free code camp, however, I found a solution that I do not understand and I am trying to put comments on each line to record as I achieve and understand it for future references and I already understand some lines but I cannot understand some parts of this code:

  function destroyer(arr) {
  // let's make the arguments part of the array 
   var args = Array.prototype.slice.call(arguments); // this would result into [[1, 2, 3, 1, 2, 3], 2, 3]
   args.splice(0,1); // now we remove the first argument index on the array so we have 2,3 in this example

 // I DO NOT UNDERSTAND THESE CODES BELOW 
  return arr.filter(function(element) { 
  return args.indexOf(element) === -1;
 });

}

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

I already check on documentation and I find it hard to understand seems the code in this sample are very different. I would really appreciate your help!

2 Answers2

1
  1. arr in the section of code you don't understand refers to the first argument passed to the destroyer function; in this case, the array [1, 2, 3, 1, 2, 3]
  2. arr.filter is using the Array.filter method to create a "filtered" version of the array with only those values that pass the "test" defined by function(element) { return args.indexOf(element) === -1; }
  3. That function uses Array.indexOf to check if the sliced args array (which you correctly identified as being equal to [2, 3]) contains the given element. Because indexOf returns -1 when the element is not found, checking for that value is equivalent to checking that the specified element is NOT in the array

The result of all of this - and the return value of the function destroy - will be the array [1, 1], representing a filtered version of the array passed to destroy that contains all the values not equal to the other values passed to destroy.

Hamms
  • 5,016
  • 21
  • 28
  • I am sorry for the late reply but it is not very clear to me as to why do we have to check this code if it is true? return args.indexOf(element) === -1; can you please tell me just a little further.. – Glendon Philipp Baculio Apr 28 '16 at 01:02
  • That code is the "test" we're using to get a filtered list. We want to get a list composed only of elements that aren't in `args`. If an element is not in `args`, then `args.indexOf(element)` will equal -1, so that's what we're checking. – Hamms Apr 28 '16 at 01:04
  • thanks I got it now, I was wondering what was happening if it was going to return false if I change the argument on destroyer, I thought we don't need === -1 for it to equal to -1 because of indexOf, which made me confused. – Glendon Philipp Baculio Apr 28 '16 at 01:13
  • this would be the last, what does return args.indexOf(element)===1; is equal to? in the case that I provide.. – Glendon Philipp Baculio Apr 28 '16 at 01:27
  • are you confused about the nature of the equality operator? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Identity_strict_equality_() – Hamms Apr 28 '16 at 01:30
  • perhaps read the documentation I linked explaining how Array.filter works – Hamms Apr 28 '16 at 01:30
  • no not with equality, the value of this return args.indexOf(element)===1; in this case, did it return [2,3]? on Array.filter()? – Glendon Philipp Baculio Apr 28 '16 at 01:36
  • `return args.indexOf(element) === -1;` returns either `true` or `false`. Read the documentation to understand how that causes the `Array.filter` call to ultimately return `[2, 3]`. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter – Hamms Apr 28 '16 at 01:37
0

Array.slice is part of the arrays prototype; prototype methods are only accessable on instances of Classes.

var arr = ['a', 'b', 'c', 'd'];
// [] is JavaScript shorthand for instantiating an Array object.
// this means that you can call:
arr.slice(someArg1);
arry.splice(someArg2);
Rhys Bradbury
  • 1,699
  • 13
  • 24