What am I missing here for wherefore art thou?

Likely something wrong with my loops but can’t figure it out. It seems source[j] isn’t logging anything

  **Your code so far**

function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line
// loop over collection
for (let i = 0; i < collection.length; i++) {
// storing first object as firstArr
  let firstArr = collection[i];
// looping through source
  for (let j = 0; j < source.length; j++) {
//checking if firstArr has object of source
    if(firstArr.hasOwnProperty(source[j])){
// pushing firstArr to arr
      arr.push(firstArr);
    }
  }
}
// Only change code above this line

return arr;
}

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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.192 Safari/537.36.

Challenge: Wherefore art thou

Link to the challenge:

source is an object, you either have to use a for...in loop or you need to use Object.keys or Object.entries to get an array of properties to loop over

1 Like

Hmm I see. I tried this but not working just yet… I got a “non-iterable instance” error

function whatIsInAName(collection, source) {
  var arr = [];
  // Only change code below this line
  for (let i = 0; i < collection.length; i++) {
    let nameSet = collection[i];
    for(let item of source) {
      if(nameSet.hasOwnProperty(source)){
        arr.push(nameSet);
      }
    }
  }

  // Only change code above this line
  return arr;
} 

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

for...of is not the same as for...in

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

Ah my fault! But still not getting the answer. Was I supposed to change anything other than “of”?

You need to check that all of the properties in the source are present in a given nameset to be able to push it

Ok can you help explain more? I’m not sure what to do or where to look to diagnose that

Could be something like this?

function whatIsInAName(collection, source) {
  var arr = [];
  // Only change code below this line
  for (let i = 0; i < collection.length; i++) {
    for (const [key, value] of Object.entries(source)) {
      if (
        collection[i].hasOwnProperty(key) &&
        collection[i]["last"] === source["last"]
      ) {
        arr.push(collection[i]);
      }
    }
  }
  // Only change code above this line
  return arr;
}

Well, you now have the ability to loop over all the properties in source, that’s what you’re doing. But you’re not waiting until you’ve checked them all before pushing the object.

No, this is not correct, it only checks that the property + one called “last” is there, and as a. source could have any number of properties, and b. “last” is just something in one of the examples, it’s just passing a few tests accidentally. You’re on the right track, but so is OP, you’ve done the same thing as them

Hmm is that because my first for loop or the for in loop? I don’t know how to make it wait I guess

I found this but I’m checking if nameSet which I thought checked all in collection to see if it has source. I tried source[i] but that didn’t change anything…

By console logging nameSet I see it goes over each collection object. So I’m assuming it’s for in but I don’t know what to change. Seems I’m checking if { first: 'Romeo', last: 'Montague' } has {last: 'capulet'} and so on.

I can’t use if(source[i].hasOwnProperty(nameSet)) because source doesn’t necessary need to contain first and last name so I’m not sure what to do

I also ended up looking at the solutions but none are taking the same approach so I’m not sure if I’m just completely off… I’m not supposed to change the var arr = []; or return arr' code yet none of the solutions are using either which seems odd

Looking at and replicating pieces of solutions is different that solving problems on your own. Continuing to practice the first will impede your ability to learn the second.

For this challenge, you need to understand both arrays and objects. Each uses different tools and approaches.

You have an array of objects. You also have a source object.

You need to check every object in the array.

You need to determine which objects in the array have all of the properties found in the source object and have those properties set to the same value.

So,

  1. how do you iterate over an array
  2. how do you iterate over all properties of an object
  3. how do you check that an object has a given property set to a particular value
2 Likes

Okay there are two pieces of logic here:

  1. you want to see if an object contains all properties that are in a second object.
  2. you want to filter an array of objects to only include objects that contain all the properties that are in a second object.

Just concentrate on 1, you’re getting confused by nested loops and things that shouldn’t matter to the part of the program you are having issues with.

Write a function that takes two objects, and checks whether all the properties (key and value) in one object are present in the other.

1 Like

Yeah so I’m not 100% of the time going to never look at an explanation or solution when I can’t figure anything else out. It’s my last choice and I’m willing to spend hours solving it without solutions, but I’m not willing to suffer for multiple days without progress. I wasn’t able to improve my code at one point and there was no choice.

I don’t want to change my approach because then I wouldn’t learn, but I genuinely don’t know what to do without using a different approach I’m seeing online. Sadly I don’t know what to do here considering your guy’s advice and my original attempt. It’d be nice to see how my code could’ve been fixed but I understand you guys don’t want to just give solutions

Just for feedback in case someone is going to read this that changes the curriculum, if the way to solve it in solutions is without return arr, I don’t think the question should tell us not to change certain parts when the only way to solve it seems to be by changing those parts.

I understand that you want to look at the answer and be able to move on. But doing so has not helped your ability to solve these problems without looking at answers. Your approach just is not helping you learn how to solve problems.

We are willing to spend dozens and dozens of posts working on fixing your code if you are willing to continue working as well. We have done it for tons of people.

If you want to make new things, then you will reach a point where there just are not solutions to look up. Continuing to look up solutions will not help you make new things.


The challenges periodically receive updates and the solutions are not always character for character identical to the current starter code. In this case, the solutions are all using filter, but if you know what a filter does, then you can replicate the exact same logic with the starter code.


In this case, what can we clarify or better explain? What part could we give more explanation on? What modifications to your code have you attempted since the third post in this topic?

You’re right. I guess maybe I should go back to the other sections and redo them again :sob: I’m not sure what (or where in my learning process) caused me not to be able to solve this on my own

Well since then I didn’t know what to do. I can think more about it again I guess, but I need a break

I really like this suggestion from Dan. Do you know how to do this? What questions do you have about this? What’s some rough code for this?


Development is all about iteration. It takes time to understand a problem, develop a plan, write some code, understand problems with your code, develop a plan to fix them, and iterate until bugs go away.

Just to be clear, you’re asking me to redo it all from scratch? I used a for loop to do this the first time. Since he suggested using for in then I assumed I was on the right path with that change

This is my new attempt using a different approach. Might’ve been influenced by a little of the things I saw but I’m trying it on my own without looking at solutions:

function whatIsInAName(collection, source) {
  var arr = [];
  // Only change code below this line
  let key = Object.keys(source);
  let values = Object.values(source);
  for(let i = 0; i < collection.length; i++) {
    if(collection[i].filter(object => object.hasOwnProperty(key) && object.hasOwnProperty(values))) {
      arr.push(collection[i]);
    }
  }

  // Only change code above this line
  return arr;
}

console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }));

Not sure why it says TypeError: collection[i].filter is not a function… I’ve also realized I’m not too familiar with the “Object.keys” functions etc. Seems straight forward but I probably just need more practice with it

I’m sorry if I implied that you need to start from scratch. That’s not what I meant.

The outer loop in your current code is fine. The inner loop needs work.

What I’m proposing is that you isolate the logic of comparing two objects into a separate helper function. Then you can adjust your code to call this helper function.

It can sametimes be easier to simplify things by breaking the problem into smaller components and composing those components.

Got it, well no worries, I still made this newer attempt on my own. I just got the key & value object functions elsewhere earlier today though.

Ok I’m not too comfortable with functions like this yet. For some reason this part hasn’t clicked. By “helper function” do you mean just make this function separately then plug it into the for loop?

Yeah I don’t know how to do the helper function. I did try to look it up

function whatIsInAName(collection, source) {
  var arr = [];
  // Only change code below this line
  let key = Object.keys(source);
  let values = Object.values(source);
  const helper = collection.filter(object => object.hasOwnProperty(key) && object.hasOwnProperty(values));

  for(let i = 0; i < collection.length; i++) {
    if(helper == true) {
      arr.push(collection[i]);
    }
  }

  // Only change code above this line
  return arr;
}

console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }));

I realize this above doesn’t make sense but I don’t know what to do. Based on my current knowledge I’m not able to turn your comments into a solution