Wherefore art thou - I'm stuck!

Wherefore art thou - I'm stuck!
0

#1

Tell us what’s happening:
I’m stuck. I don’t know what else to try. How to check if an object from collection has the same value as source? Should i even check that or something else? please help.

Your code so far

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  var keys = Object.keys(source);
  var val = Object.values(source);
  var dr;
  // Only change code below this line
  arr = collection.filter(function(o){
    for(var i=0; i < keys.length; i++)
      return o.hasOwnProperty(keys) && Object.is(o[keys[i]],val[i]);
  });
 
 /* for(var i=0; i < collection.length; i++){
    for(var j=0; j < val.length; j++){
      if(collection[i].hasOwnProperty(keys[i]) || Object.is(collection[i][keys[j]],val[j])){
        arr.push(collection[i]);    
      }  
    }
  }*/
  // Only change code above this line
  return arr;
}

whatIsInAName([{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }], { "a": 1, "b": 2 });```
**Your browser information:**

Your Browser User Agent is: ```Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36```.

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

#2

Using the example call:

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

you declare keys to be an array which looks like:

[ 'a', 'b' ]

In the first part of your return statement, you have:

o.hasOwnProperty(keys)

which is always going to evaluate to false, because each element in the collection array is an object. You are are asking if the object (o) has a property named [‘a’,‘b’].


#3

ok , so i did this:

return o.hasOwnProperty(keys[i]) && Object.is(o[keys[i]],val[i]);

first and second tests are good, 3rd and fourth still dont return the right value.


#4

I finally did it somehow, but im not sure if its right. Thank you mr. rmdawson17 for your time.
Now i can go to sleep :smiley:

function whatIsInAName(collection, source) {
  var arr = [];
  var keys = Object.keys(source);
  var val = Object.values(source);
  arr = collection.filter(function(o){
    for(var i=0; i < keys.length; i++)
      return o.hasOwnProperty(keys[i]) && Object.is(o[keys[i]],val[i]) && Object.is(o[keys[i+1]],val[i+1]);
  });
  return arr;
}

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

#5

You did pass the challenge, but your solution kind of “cheated”, because you hard coded it to handle an only cases where the 2nd argument object has 1 or 2 properties. All the FCC test cases only had one or two properties in the object.

If they would have use the following test case:

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

your solution would return:

[ { a: 1, b: 2 }, { a: 1, b: 2, c: 2 } ]

and the correct result is:

[ ] // an empty array, because there is not a property named "d" in any of the objects of the collection array

#6

There are multiple ways to do this, an easy one to follow is to loop over the keys until you find the one that does not match, and return false at that point, if your code gets through the loop and all the key/value pairs match then you return true. This concept is called early return, same idea could be used by having a flag variable before the loop that defaults to true, and is set to false if any of the key/pairs don’t match. After the loop you can return that flag variable for your filter. A sample of early return can be found in mdn in the section interrupt a function.

Another way is a handy function from the Array object called every, with this function you can test all the elements of an array, and if all the tests are true, it returns true and if any of the tests is false it returns false. An easy way to replace the loop completely with a call to every. Check it out in mdn.

Hope those help you to get a more complete solution


#7

thank you guys. i knew my solution is not good.