Intermediate Algorithm Scripting - Wherefore art thou (Stumped)

Tell us what’s happening:
I was making some progress (this passes the first 3 tests), but I have been at a standstill now for hours. I tried to comment in my thought process throughout. I believe my problem is with my callback function for the filter. specifically, my callback function is not vetting properly. Even more specifically, after some testing, I think it has something to do with the if statement in my for..in loop. I’m gonna take a break from this one, the word “source” is staring to look weird to me haha. I’m considering scrapping the whole thing and trying another method. End vent - actual questions:

  1. Is the method I am trying here sound? Should I just scrap it an try anew?

  2. If my methodology is sound, is there some simple mistake I am making in my callback function?

Thanks

Your code so far


function whatIsInAName(collection, source) {
 
 //callback function for filter
  function propCheck(collectionObj) { 
    for(let sourceProperty in source) {   //check each property in 'source'
      if (collectionObj.hasOwnProperty(sourceProperty) && 
      (source[sourceProperty] === collectionObj[sourceProperty])) { 
        return true;  // filter accepts IF all props/values (from source) are in collection
      } else {
        return false;  //otherwise filter will reject
      }  
    }
  }

  // the filter will create a shallow copy, 'filteredNames'
  let filteredNames = [];
  return filteredNames = collection.filter(propCheck);  
    // return the shallow copy
}

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/109.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Wherefore art thou

Link to the challenge:

This immediately stops the callback function (executing zero additional loop iterations)

1 Like

Oh my goodness. That was a saga. Lesson learned. Thanks for confirming my suspicion and pointing me in the right direction. Finally got it like this:

      if (!collectionObj.hasOwnProperty(sourceProperty) || 
         (source[sourceProperty] !== collectionObj[sourceProperty])) { 
          return false;
      }
    }
    return true;
  }
1 Like

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