Please check my code. I wrote everything correctly, my code doesn't work

Tell us what’s happening:

Your code so far


function whatIsInAName(collection, source) {
let src = Object.keys(source)

 return collection.filter((el) => src.map((k) => el.hasOwnProperty(k) && el[k]=== source[k])).reduce((a, b) => a && b);
}

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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36.

Link to the challenge:

Your function only returns 1 element… You could try to be more clear writing your code; saving space is almost never useful… Your code must be readable when you are working with others.

1 Like

Hi, the reduce method should be concatenated after the map function instead of the filter one.
This is the working code:

let src = Object.keys(source);

 arr = collection.filter(
   (el) => {
     return src.map((k) => el.hasOwnProperty(k) && el[k] === source[k]).reduce((a, b) => a && b);
   });

Thank you very much…