Wherefore art thou algorithm/code issues

i try a similar condition like you and do not get two cases, i understand but i am not sure why my code not pass all cases

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

  for(let index = 0; index < dataKeys.length ; index++) {
    arr = collection.filter( (elem) => {
      //console.log(elem[dataKeys[index]]);
      return ((elem[dataKeys[index]] === dataValues[index]) && (collectionKeys.length >= dataKeys.length));
    });
  } 
  
  // Only change code above this line
  console.log(arr);
  return arr;
}

whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 });

the case not pass are:
whatIsInAName([{ “apple”: 1, “bat”: 2 }, { “bat”: 2 }, { “apple”: 1, “bat”: 2, “cookie”: 2 }], { “apple”: 1, “bat”: 2 })should return[{ “apple”: 1, “bat”: 2 }, { “apple”: 1, “bat”: 2, “cookie”: 2 }]

whatIsInAName([{ “apple”: 1, “bat”: 2 }, { “apple”: 1 }, { “apple”: 1, “bat”: 2, “cookie”: 2 }, { “bat”:2 }], { “apple”: 1, “bat”: 2 })should return[{ “apple”: 1, “bat”: 2 }, { “apple”: 1, “bat”: 2, “cookie”:2 }]

i do that but i am missing something in my filter condition

can you post your updated code?

collectionKeys does not seem to be defined

Even so, after tracing you loop I believe that your algorithm will filter any collection object that matches any of the source name/value pairs.

You need to test that an object from collection matches all name/value pairs in source before including it in the resultant array.

From the instructions

Each name and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array.

function whatIsInAName(collection, source) {
// What’s in a name?
let arr = [];
// Only change code below this line
let dataKeys = Object.keys(source);
let dataVales = Object.values(source);
let collectionKeys = Object.values(collection);

for(let index = 0; index < dataKeys.length ; index++) {
arr = collection.filter( (elem) => {
//console.log(elem[dataKeys[index]]);
return ((elem[dataKeys[index]] === dataValues[index]) && (collectionKeys.length >= dataKeys.length));
});
}

// Only change code above this line
console.log(arr);
return arr;
}

i do that check my problem when i print arr before return make and aditional mach with isoletes elementos when match one of the porperty of the source into collection

The logic of your algorithm is giving you incorrect results.
You are doing the right things but you are doing them in the wrong order.

Do you understand why your code returns {bat:2} on the third test?

[ { apple: 1, bat: 2 },
{ bat: 2 },
{ apple: 1, bat: 2, cookie: 2 } ]

Do you understand why {bat:2} should not be in arr?

i understand why {bat:2} should not be in arr because is not a complete match bt i check for each key-value
i try usig hasOwPropert() function but do not works

Looking at your for loop executing the third test

On the first pass any collection object that contains apple:1 is put into arr
arr is [ { apple: 1, bat: 2 }, { apple: 1, bat: 2, cookie: 2 } ]

On the second pass arr is emptied! and then any collection object that matches bat:2 is put into arr
arr is [ { apple: 1, bat: 2 }, { bat: 2 }, { apple: 1, bat: 2, cookie: 2 } ]

then arr is returned

@rabindranathfv

i see, i will try anoter again…thx

Excellent! Post again if you get stuck