Wherefore art thou | Intermediate Scripting

The first two test passed. But the others aren’t. I am not sure where the problem is coming in?

function whatIsInAName(collection, source) { 
  const arr = [];
  const sourceKeys = Object.keys(source)
  
  //console.log(collection[0][sourceKeys[0]])
  for(let i = 0 ; i < collection.length; i++) {
   
    for(let j = 0; j < sourceKeys.length; j++){
      const sourceKey  = source[sourceKeys[j]]
       
      if(collection[i][sourceKeys[j]] === sourceKey) {
        arr.push(collection[i])
        }
     } 
   }
console.log(arr);
  return arr;
 }

Problem link - https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou

Thanks :slight_smile: :slight_smile:
Deeti

You’re not really checking whether every name and value pair of the source object is present in the object from the collection.

I had trouble on this on as well. Let me break your function into the steps it is solving.

function whatIsInAName(collection, source) { 
  const arr = [];  // return array
  const sourceKeys = Object.keys(source)  // you can't access
// the properties on an object without you knowing the keys
// so you make an array of the keys
  
  for(let i = 0 ; i < collection.length; i++) { //look through the
// collection to search all the objects
 
    for(let j = 0; j < sourceKeys.length; j++){ // searching 
//through all the keys to check that they all fit
      const sourceKey  = source[sourceKeys[j]] 
       
      if(collection[i][sourceKeys[j]] === sourceKey) { 
// see if the key exists
        arr.push(collection[i]) // push to answer array
        }
     } 
   }
console.log(arr);
  return arr;
 }

This works only to see if the key is in the object:

Each name and value pair of the source object has to be present 
in the object.

Some helpful functions.

Object.entries(source) // returns an array of the key value pairs
// ordered like this [[key, value], [key2, value2], etc]
Object.values(source) // returns an array of values

Hope this helps you