Should this be happening? - Algorithm Scripting

It looks like you’re just pushing the first element of the collection to the array and then returning the array.

1 Like

This is the challenge --> https://www.freecodecamp.org/challenges/wherefore-art-thou

Again the code i’m using isn’t working because its checking if it exactly is, not containing.

@John-freeCodeCamp Can you reason out why it’s checking exactly instead of if they have a value in common?

1 Like

I honestly don’t know why its not giving me objects in which it partially contains. Is their also anyway i can check the value too?

Should i make official forum post about this for better information?

@John-freeCodeCamp

Earlier in your program sourceNames gets set to the properties in your source parameter. So you have multiple values you want to check again in both the collectionNames and the sourceNames. You’re going to need another loop because right now you’re only cycling through the collectionNames, whereas you need to be iterating through both collectionNames and sourceNames. I think your first piece of code contains two loops which is the direction I would go.

I would use console.log a whole bunch so you know what you’re working with at any given time. I took your code copied it into a jsbin in order to see what you were doing. Here is a screen shot:

1 Like

Should i still use that code? I fix it and it only works for one and not the other, vise versa.

They both have different lengths, so how would i compare them? Am i getting to over complicated?

@John-freeCodeCamp

Using two loops to compare different arrays looks like this:

for (var i = 0; i < someArray.length; i++) {
  for (var j = 0; j < someOtherArray.length; j++) {
    if (someArray[i] === someOtherArray[j]) {
      // this is where you would push to the array you are keeping matching values in
    }
  }
}
1 Like

My code previously has one flaw. It checks individually. fior { a: 1, b: 2 } it would check if the other object contains a 1 and then b 2 another time.

@John-freeCodeCamp
You aren’t working with objects because you turned them into arrays with the getOwnPropertyNames method you’re using. If you want to check for objects you’ll want to use

for (variable in obj) {
  // do something for each key in the object

}

It seems like what’s really stopping you from solving the problem is turning them into arrays. I would look up the docs for the “for…in statement” because you can use that to check the properties and the values of those properties.

1 Like
for (var j = 0; j < collection.length; j++) {
        for (let i in collection[j]) {
            console.log(i)
        }
    }

This is a start. Only gets the properties and not values.

That a regular for loop not the for…in loop I suggested.

1 Like
for (let i in collection) {
        console.log(i)
    }

This grabs how many elements are in collection…

This is how you use it. Inside of the for in loop you need to ask it to show you the values. So “key” is the property and obj[key] is the value.

1 Like

Okay ill try to use that. I came up with this, is it usable in anyway?

for (let i in collection) {
        if (Object.values(collection[i]) === Object.values(source)) {
            return collection[i]
        }
    }

Or should i just use the example you just gave me as a start?

I’d need to see it in the greater context of the program but it still looks like you’re comparing the collection[i] value to the entire source object when what you really want to do is check to see if each property and subsequent value in the source object match any part of the collection object.

1 Like

So how do i compare both properties and their values?

This is how you would see what properties and values two objects have in common.

Don’t worry that it printed out twice that is something I did not what the code is doing. Hope it helps.

1 Like
function whatIsInAName(collection, source) {
    var array = [];
    for (let i in collection) {
        for (let j in source) {
            if (j === i && collection[i] === source[j]) {
                console.log(i, collection[i])
            }
        }
    }
}

whatIsInAName([{ a: 1, b: 2 }, { a: 1 }, { a: 1, b: 2, c: 2 }], { a: 1, b: 2 });

How come nothing appears in the console?

Because collection is still an array at this point.

Looks like you’ll need a regular for loop and then inside of the regular one put the two for…in loops like you have them.

function whatIsInAName(collection, source) {
  var array = [];
  for (var i = 0; i < collection.length; i++) {
    for (let j in collection[i]) {
      for (let k in source) {
        if (j === k && collection[i][j] === source[k]) {
          console.log(j, collection[i][j])
        }
      } 
    }   
  }
}
2 Likes
function whatIsInAName(array, object) {
    return array.filter(function(o) {
        return Object.keys(object).every(function(k) {
           return k in o && object[k] == o[k]
        });
    });
}

I saw this solution somewhere, and this is what it does based on my knowledge. It filters array, goes through the keys of the object (what you are looking for), and for every key it checks if the property names are the same, and if their values are the same (by checking if the value of the key in object is the value of the key in array).