Wherefore art thou? Any tips?

I am tearing apart my brain on this challenge. Could anyone possibly point me in the correct direction? Here is my code so far:

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  var result;
  var prop = Object.keys(source);
  arr = collection.filter(function(obj) {
    for (var i = 0; i < prop.length; i++) {
      if (obj.hasOwnProperty(prop[i]) && obj[prop[i]] === source[prop[i]]) {
        result = true;
      } else {
        result = false;
    return result;
  // Only change code above this line
  return arr;

whatIsInAName([{ “apple”: 1, “bat”: 2 }, { “bat”: 2 }, { “apple”: 1, “bat”: 2, “cookie”: 2 }], { “apple”: 1, “bat”: 2 }) should return [{ “apple”: 1, “bat”: 2 }, { “apple”: 1, “bat”: 2, “cookie”: 2 }].

You are failing this test case because in your if and else statements, when it loops through the firs time, result is false but when it runs on the 2nd key, result becomes true therefore returning true in the filter method. Hopefully this helps you progress, come back if you are still stuck.

Good luck!

1 Like

A return statement halts execution of a function and returns a value.

Your code tests the first property and either returns true or false based on that one test.
if(test is true) return true, else then return false
True or false your function ends the first time the if statement executes.

You need to test to test each of the properties. If one is not a match then you can quit testing and return false. But only when you have tested all properties and none are false can your return true.

1 Like

I’d like to make an edit to your explanation. Rather than “halt”, i’d say “exits” out of because halt may imply that it will resume.

1 Like

Thanks everyone! It finally clicked that in some cases I needed to exit the for loop.