Wherefore art thou- difficulty with if then statement within .every() method

Tell us what’s happening:
Problem is current object within collection is pushed to the array even if the current object doesn’t have ALL the properties in source object.

I believe it is because of my if then statement but i am unsure how i can restructure the .every(callback function) method.

Code is commented to the best of my ability.
Your code so far


function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line

//each object in collection is filtered by callback function where "current" is the current object 
collection.filter((current)=>{

/*
Object.keys(source): converts source object into array 
.every(): array method (hence previous step). method tests whether all elements in the array pass the callback function
sourcekey: current key in source array (aka current property key in source object) 
*/  
Object.keys(source).every((sourcekey)=>{

    //if current object has source's sourcekey AND current key value is equal to sourcekeys key value then 
    if((current.hasOwnProperty(sourcekey)) && (current[sourcekey] === source[sourcekey])) {
    arr.push(current);//push current object into array
    //THIS IS THE PROBLEM: even though this is within the .every() callback function, it pushes the current object even if not sourcekeys pass callback
    //its becuase of the if then statement but unsure how to change

    }
  }) 
 });

// Only change code above this line
return arr;
}

console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }));

//NOTE Object.is() won't work
//can't use dot notation because of variables 

Your browser information:

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

Challenge: Wherefore art thou

Link to the challenge:

You have a bunch of logic inside your .every(), but you aren’t actually writing an .every() function that works. The callback function in .every() performs the test that you want to put every element through and returns a boolean.

that makes sense. So is .every() not feasible? If so, is there another method you suggest?

.every() can be used here. You just aren’t using it correctly. You will only know if all of the properties are present after the .every() command has completed. .every()returns a boolean that tells you whether every item passes the test. You are using it more like a .forEach().

Check the MDN docs to get a better understanding of the method.

Here is a video as well.

Okay so my new solution is very different but I believe the main issue was trying to combine too much in the .every() method.

function whatIsInAName(collection, source)
{
  // What's in a name?
  var arr = [];
  // Only change code below this line
 let sourcekeys=Object.keys(source);
 collection.filter(current=>{
   let currentHasAllProps= sourcekeys.every(key=> current.hasOwnProperty(key));
   let propValsEqual=sourcekeys.every(key=>source[key]===current[key]);
   if (currentHasAllProps ===true && propValsEqual===true) {
     arr.push(current);
   }
 }) 
  // Only change code above this line
  return arr;
}

console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }));```

I’m glad that your code works, but you’re now using a filter as a forEach. You’re not actually filtering collection.