Intermediate Algorithm Scripting - Wherefore art thou

Tell us what’s happening:
After six hours of no progress I have to admit there has to be something very basic that I’m missing here. I tried going back to Functional Programming and OOP parts for ideas but I cant seem to find a way to access the objects key:value pairs nested in the array.
If I understood the of the idea of the challenge here, I should be checking the objects inside the array if their keys have a matching value.

What I do know is that my current code isn’t accessing the object because .forEach is missing the correct index.

I’m literally so lost I can’t even think of questions to ask that could help me get on with the challenge.

Any pointers which previous challenges I should revisit?

Your code so far

function whatIsInAName(collection, source) {
  let toReturn = [];
  let sourceKey = Object.keys(source)
  let sourceValue = Object.values(source)

  let x = collection
    .filter(element => element.hasOwnProperty(sourceKey))
    .forEach(element => element.first === sourceKey || element.last === sourceKey);

//check if objects in collection array has sourceKeys in it

  /*for (let i = 0; i < collection.length; i++) {
    if (collection[i].hasOwnProperty(sourceKeys) === sourceKeys) {

  { first: "Romeo", last: "Montague" },
  { first: "Mercutio", last: null },
  { first: "Tybalt", last: "Capulet" },
  { first: "asd" }
  { last: "Capulet" });

  { "apple": 1, "bat": 2 },
  { "bat": 2 },
  { "apple": 1, "bat": 2, "cookie": 2 }
  { "apple": 1, "bat": 2 });*/

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Intermediate Algorithm Scripting - Wherefore art thou

Link to the challenge:

See if you can describe the algorithm to us that you think will solve the challenge before writing any further code.

Let’s give it a go.

  1. Set contents of collection and source into a variable and find out what they have stored in them.
  2. Check if first object in collection has the same key or keys as source.
  3. If keys match, check if values stored in keys match. If keys do not match, jump to next index of collection.
    (If multiple sources are given as arguments, check that all keys are found.)
  4. If values match, store the whole object from collection into new array waiting to be returned. If values do not match, jump to the next index of collection.
    (If multiple sources are given as arguments, check that all values match.)
  5. Go to next object in collection array.
  6. Repeat steps 2 to 5 until all indexes of collection have been checked.
  7. Return new array with objects from whatIsInAName function.

Is this what you were asking me to do?

function whatIsInAName(collection, source) {
  let sourceKey = Object.keys(source)
  let sourceValue = Object.values(source)
  /*console.log(sourceKey + " sourceKeys", sourceValue + " sourceValues")
  console.log(collection[0][sourceKey[0]] + " this is the value in sourceKey[index]")*/
  /*let arr = collection
    .filter(element => element.hasOwnProperty(sourceKey))*/


  let tester = [];

  if (collection[0].hasOwnProperty(sourceKey[0]) && collection[0][sourceKey[0]] === sourceKey[0]) {
    console.log("it works")

//whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }, {first: "Test"}], { last: "Capulet" });
    { "apple": 1, "bat": 2 },
    { "bat": 2 },
    { "apple": 1, "bat": 2, "cookie": 2 }
  { "apple": 1, "bat": 2 }

More than 12 active hours in with this challenge and this is what I have came up this far, which is nothing.

I can check the values stored in the indexes of collection and source but I can’t find a way to check the name of any of the keys in objects stored in collection.
Also I can’t compare if key:value pair in source has the same value as the key:value pair in collection. I only get to log that sources key is apple and collections value is 1 but they are not comparable.
Because of this I can’t figure out how to make my if-statement work. Also I haven’t introduced in any for loops yet as I can’t figure out how to check for the test requirements.

I’ve read a lot of documentations considering objects and arrays including their different methods. I’ve read through a lot of forum posts relating to this challenge. I’ve even checked out the solutions but I can’t figure how they work as they are super condensed minimalistic code.

What previous exercises should I check to be able to complete this one?
I’m totally stumped with this challenge.

Also I know I have to study how functions inside functions work, what are callback functions and how arrow functions operate so any good material on these topics?

You’re making this more complicated than it needs to be, which is totally normal.

The collection and source are already variables. No need to do this.

This is a big step here. How do you do this?

This sounds better

I think you are not really writing isolated steps here

The steps you gave above seem to have a couple of loops implied but you have no loops here. This one line is close to what you want at heart of those loops though.

This needs a small fix though

collection[0][sourceKey[0]] === sourceKey[0]

What does this literally say?

How am I not? I don’t understand.

Yes there are implied loops but I haven’t even started with the loops as the main functionality isn’t there. My intention is to replace index numbers with for loop accumulators.

It says 1 is strictly equal to apple, which it isn’t. The problem is I can’t figure out what my code is missing to access the objects value in collection and not the key.

This is the value corresponding to the key in the collection

What is this though?

You have two different ‘steps’ saying what to do if the values don’t match. Steps shouldn’t overlap - that makes them harder to convert to code.

What is this though?
It is the first index in sourceKey array containing apple.

What is the difference to write them separately or together? If I’m correct I need to check that collection has same keys as source has and after that I need to check that the values bound to the keys in collections have the same data stored in them as values bound to the keys of source.

So why compare it with the corresponding value out of the first object in the collection? Comparing a key and a value won’t match. What is this comparison supposed to do again?

This checks that the key is present on the first object in the collection.

The whole point behind writing out steps is that they become an outline for your code. If your steps say the same thing, then it isn’t really an outline and you need to write code that doesn’t match the steps.

That is the problem I’m trying to tackle here. I want to compare the values of collection and source, not to compare the key of source to the value of collection. I can’t figure out how to do it.

Isn’t it the first part to check that source and the object in collection have the same key? If they do second step is to check that the values related to keys match.

Right, and that part checks if the first collection object has the key

Right, which brings us to here

How did you get the value associated with the key for the first object in the collection? Finding the value associated with the key for the source is very similar.

I actually don’t know how to do it.


gives me 1 which is the value of the key “apple” from collection, right?

I’ve tried accessing the value of source with:


What is it I’m not seeing here.

How does this line work? What does each part of the syntax do?

You can’t use dot notation when the key name is stored in a variable

What is source[0]? Is source an array?

It first reads index 0 from collection array. First item is an object that has “apple”: 1 and “bat”: 2 in it from where it reads the first index being key “apple” and returns the value stored in the key “apple”.

1 Like

Which brings us back around to the question - is source on array?

‘what type of thing is this’ is a critical piece of knowledge about every variable.

No, source isn’t an array, it’s an object.
I still don’t understand how should I access it as neither dot notation nor bracket notation can do it here.

If it isn’t an array, why the [0] here?

To access the first key:value pair as there can be infinite number of them.

That’s wdat the second [0] is for. What is the first one for?