Intermediate Algorithm Scripting - Wherefore art thou

Tell us what’s happening:
Describe your issue in detail here.
In here I return true or fasle, but when i console.log this : whatIsInAName([{ first: “Romeo”, last: “Montague” }, { first: “Mercutio”, last: null }, { first: “Tybalt”, last: “Capulet” }], { last: “Capulet” }), it returns [{ first: “Tybalt”, last: “Capulet” }]. Because I thought that it only return the boolean value, so why does that work, can you explain it to me? Thank you.

   **Your code so far**
function whatIsInAName(collection, source) {
 let sourceKeys = Object.keys(source);
 // console.log(sourceKeys)
return  collection.filter(function(obj) {
 //  console.log(obj)
   for(let key of sourceKeys) {
     // console.log(key)
     // console.log(obj[key])
     // console.log(source[key])
     if(!obj.hasOwnProperty(key) || obj[key] !== source[key]) {
       return false;
     }
   }
   return true;
 })
}

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 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Wherefore art thou

Link to the challenge:

What is the first return statement in your function returning? The return statements that return either true or false are part of the filter callback, they tell the filter whether to keep the item in the array. They aren’t returned by whatIsInAName.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.