A hint on logic, truth tables and operators

Tell us what’s happening:
Hi,

as you can see from the code below I tried to solve the challenge with a filter + for() = if() but the logic fails.
In my reasoning if the keys of the source object exists in the collection object and their values are the same return true so that the filter return that specific object tat passed the case. So I coded but for the given collection/source it seems that everyone pass the case. I also tried a truth table and I came out with the fact that on the second loop of the second object checked by the filter() it should not find the key “bat” obj.hasOwnProperty(sourceKeys[i]) returning false, exiting the condition and not being present in the filtered array.
But not.
Any hint?
Thank you
L

Your code so far


function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line
let sourceKeys = Object.keys(source);
//console.log("1: " + sourceKeys);
//console.log("2: " + source[sourceKeys[0]])

arr = collection
      .filter(function (obj){
        for (let i = 0; i < sourceKeys.length; i++){
          //console.log(source[sourceKeys[0]])
          //console.log(obj[sourceKeys[0]])
          if( obj.hasOwnProperty(sourceKeys[i]) && 
              (obj[sourceKeys[i]] == source[sourceKeys[i]])
            ){
            return true
          }
        }
        return false
})
// Only change code above this line
return arr;
}

console.log(whatIsInAName(
[
{ "apple": 1, "bat": 2 }, 
{ "apple": 1 }, 
{ "apple": 1, "bat": 2, "cookie": 2 }, 
{ "bat":2 }
], 

{ "apple": 1, "bat": 2 }))


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.

Challenge: Wherefore art thou

Link to the challenge:

Hello there,

I think you will see the problem, if you use this:

arr = collection
      .filter(function (obj){
        for (let i = 0; i < sourceKeys.length; i++){
          console.log(source[sourceKeys[i]])
          //console.log(obj[sourceKeys[0]])
          if( obj.hasOwnProperty(sourceKeys[i]) && 
              (obj[sourceKeys[i]] == source[sourceKeys[i]])
            ){
            return true;
          }
        }
        return false;
})

Hope this helps