FreeCodeCamp Wherefore Art Thou Solution

FreeCodeCamp Wherefore Art Thou  Solution



function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  //return Object.keys(source);

  var sourceKeys = Object.keys(source);
  var sourceValues = Object.values(source);

  for(var i=0; i<collection.length; i++){
    var collectionKeys = Object.keys(collection[i]);
    var collectionValues = Object.values(collection[i]);
 
    var allKeysTrue = true;
    var allValuesTrue = true;
    for(var j=0; j<sourceKeys.length; j++){
   
      if(!collectionKeys.toString().includes(sourceKeys[j].toString())){
        allKeysTrue = false;
      }
   
      if(!collectionValues.toString().includes(sourceValues[j].toString())){
        allValuesTrue = false;
      }
   
    }
    if(allKeysTrue && allValuesTrue){
      arr.push(collection[i]);
    }
  }
  // Only change code above this line
  return arr;
}

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

Comments

Popular posts from this blog

Basic CSS: Use Attribute Selectors to Style Elements

Basic CSS: Use Clockwise Notation to Specify the Padding of an Element

Basic CSS: Use Clockwise Notation to Specify the Margin of an Element