Don't understand the Wherefore Art Thou Challenge

Don't understand the Wherefore Art Thou Challenge
0.0 0

#1

Can someone please explain this to a layman. I really don’t understand the basic solution of the Wherefore Art Thou challenge. Specifically, I don’t understand this block of code



return collection.filter(function(obj){
      for(var i=0;i<srcK.length;i++){
        if(!obj.hasOwnProperty(srcK[i]) || obj[srcK[i]] !== source[srcK[i]]){
          return false;
        }

#2

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  //Object.keys finds the property for a given object(all listed)
  
  var srcK = Object.keys(source);
  // Only change code above this line
  return collection.filter(function(obj){
      for(var i=0;i<srcK.length;i++){
        if(!obj.hasOwnProperty(srcK[i]) || obj[srcK[i]] !== source[srcK[i]]){
          return false;
        }
      }
     return true;
  });
}

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

This is all of the code, but the above is what I’m confused about.


#3

If you read the code explanation below the solution, it does a good job of explaining what the code is doing. What specifically do you not understand about this section of code? The filter function? The for loop declaration? The if statement? The return false?


#4

(!obj.hasOwnProperty(srcK[i]) || obj[srcK[i]] !== source[srcK[i]]) this specifically. I know it’s supposed to return false, but I don’t understand the syntax


#5

There are two conditions in this if statement:

The first is:

!obj.hasOwnProperty(srcK[i])
// The above is asking if obj does NOT have a specific property.  
// The specific property is contained in srcK[i]

The second is:

obj[srcK[i]] !== source[srcK[i]]
// The above is asking whether value of obj's srcK[i] property is NOT equal to
// the value of source's srckK[i] property.

The two conditions are separated by the || operator, so if at least one of these conditions is true, then the block of code followed by this if will be executed (in this case return false;)


#6

The syntax is saying if the object does not (!) have property srK[i] OR the value of obj[srcK[i]] is not equal to the value of source[srcK[i]]).