Wherefore art thou - Confused

Tell us what’s happening:
I’m not sure how to ask my question. I don’t want to look at other threads on Wherefore Art Thou or the solution. I think it might “give it away”. So I need a hint beyond the hints given. I know how to loop through an object, but I can’t wrap my head around how to know if the key-value pairs are in the collection. The solution should be, if the source key-value pairs are contained in the collection object, then return the collection object.

I know the hasownproperty will return true/false based on if the key exists. Object.keys will return the key of the object.

Grrrr. Not sure why it isn’t clicking.

Thanks in advance.

Your code so far

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
   //Does each element in collection have 'all' of key/value pairs of source?
  // Only change code below this line
 for (var i =0; i < collection.length; i++){
   console.log(collection[i]);
 }
 
  
  // Only change code above this line
  return arr;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/wherefore-art-thou

Go here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

and get familiar with some array methods. This should give you some ideas on how to solve this.

1 Like

Here’s a couple of sub-challenges that may help:

inCollection

Check an array for a given item. Return true if it’s in the array, false if it’s not.

The challenge

inObject

Check an array of objects for a given property. Return true if it’s in any of the objects, false if it’s not.

The challenge

All you’re doing is returning true or false. If you can do these, you should be able to do Wherefore art thou.

4 Likes

Thanks @elisecode247 & @PortableStick.

@PortableStick - For the first one (in collection) I have:
https://repl.it/repls/HorribleNumbBlockchain
It seems to work but I don’t know where the undefined is coming from. Any insight to where this is coming from?

forEach doesn’t return anything, no matter what you return from its inner function. Think simple for now.

1 Like

Simpler than a for loop like?:
https://repl.it/repls/SmoggyUpbeatPatch

You’re very close, but that code won’t return true. Comment out the last function and you’ll see that it returns false. Think about what it would mean to return while inside a loop.

1 Like

It would exit the function and stop the loop from further processing. Hmm.
https://repl.it/repls/QuietTriangularEquations
So, only let it exit if it’s true OR wait until it checks the length of the array and if it’s not true, it must return false?

Bingo. Try the next one.

1 Like

Something like:
https://repl.it/repls/TransparentCoolCarriers ?

Yup, good. Now the main challenge is a little more complicated because you have to compare both a property and a value, but I think you can do it.

2 Likes