It's hard to understand why this way works but not other way

Tell us what’s happening:
I was stuck with this test and I found a code on forum to pass but what I made was having this part

if(source[key] === obj[key]){
return true; } // then
return false;

I didn’t understand why it doesn’t work… Is there somebody who can help my ignorance?
Thank you!

Your code so far


function whatIsInAName(collection, source) {
var arr = [];
arr = collection.filter(function(obj){
  for(var key in source){
    if(source[key] !== obj[key]){
      return false;
    }
  }return true;
})
return arr;
}
//왜 if 에서 source[key]=== obj[key] 를 두고 하면 안될까?.//
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.

Challenge: Wherefore art thou

Link to the challenge:

this will return true if just one is equal, it will not check all of them

2 Likes

I keep forgetting that when I use “if” … gosh.
Thank you for the answer. I will try not to forget this again.
Happy Coding @ieahleen !!