Intermediate Algorithm Scripting - Wherefore art thou

Tell us what’s happening:

Describe your issue in detail here.
Am I thinking wrong here?
I am trying to filter those collection object where every provided source key is equal to some of the keys of collection object and every provided source value is equal to every key from collection object

Your code so far

function whatIsInAName(collection, source) {


return collection.filter(obj=>{
  let collectionKeys=Object.keys(obj)
  let collectionValues=Object.values(obj)
  let sourceKeys=Object.keys(source)
  let sourceValues=Object.values(source)

//  console.log(collectionKeys,collectionValues)
//   console.log(sourceKeys,sourceValues)

  return sourceKeys.every(sKey=>{
    return collectionKeys.some(cKey=>{
      // console.log(sKey,cKey)
      return sKey===cKey
    }) 
    && sourceValues.every(sValue=>{
      // console.log(sValue)
      return collectionValues.every(cValue=>{
        return sValue===cValue
      })
    })
  })
    

})

}

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

console.log(res)

Your browser information:

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

Challenge Information:

Intermediate Algorithm Scripting - Wherefore art thou

I think you’ve tripped yourself up making their filter callback really complex.

I suspect the problem is in this part. What does it do exactly?

2 Likes

Here I am trying to see if every value of source object matches with that of collection object

But it literally says that every single value must be the same value. But that’s not what you want. and really, it doesn’t matter if two different values agree - you need to know if the property’s values agree.

Do u mean to say I shall only check obj properties and not its values

No. I don’t mean to say u should not check the values of the properties. U have to check the values of the properties. But u need to check if the values of the properties match the source object’s values for the exact same property.

Ya I got it means I need to use different solution than using some and every.

Right. Some and every aren’t checking that the same property exists in the target object with the exact same value.

1 Like

Whats going wrong now?

function whatIsInAName(collection, source) {
return collection.filter(obj=>{
for (let key in source)
return obj.hasOwnProperty(key) && obj[key]===source[key] 

})
}
let res=whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
let res2=whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 }) 

console.log(res)
console.log(res2)


Can you say more?

return statements immediately stop loops.

Am I doing basic mistake? I am forcing myself not to see any solutions because I want to build logic on my own

function whatIsInAName(collection, source) {
  
return collection.filter(obj=>{
let check;
for(let key in source){
   console.log(key)
check=obj.hasOwnProperty(key) && obj[key]===source[key]
}
console.log(check)
   return check
})
}



let res=whatIsInAName([{"a": 1, "b": 2, "c": 3, "d": 9999}], {"a": 1, "b": 9999, "c": 3})

console.log(res)

Better, but now check only depends upon the last key-value pair; you need to combine the last result and the current result inside of the loop.

1 Like

I am sorry, I have seen the solution, because I was stucked at it since past 2 to 3 days, but I am still not getting what I was missing.
This is final solution though.
Why it was failing when i was trying to filter true values.

function whatIsInAName(collection, source) {
  
return collection.filter(obj=>{
let check=true;
for(let key in source){
   if(!obj.hasOwnProperty(key) || obj[key]!==source[key]){
      check =false
      break
   }
   
}
   return check
})
}



let res=whatIsInAName([{"a": 1, "b": 2, "c": 3, "d": 9999}], {"a": 1, "b": 9999, "c": 3})

console.log(res)