Am I right in thinking that if the boolean returns as true then it automatically puts the values into a new array? I’m not 100% sure I understand why booleans are used here.
Your code so far
function whatIsInAName(collection, source) {
// "What's in a name? that which we call a rose
// By any other name would smell as sweet.”
// -- by William Shakespeare, Romeo and Juliet
const souceKeys = Object.keys(source);
// filter the collection
return collection.filter(obj => {
for (let i = 0; i < souceKeys.length; i++) {
if (!obj.hasOwnProperty(souceKeys[i]) ||
obj[souceKeys[i]] !== source[souceKeys[i]]) {
return false;
}
}
return true;
});
}
// test here
whatIsInAName(
[
{ first: "Romeo", last: "Montague" },
{ first: "Mercutio", last: null },
{ first: "Tybalt", last: "Capulet" }
],
{ last: "Capulet" }
);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36
Challenge: Intermediate Algorithm Scripting - Wherefore art thou
Link to the challenge: