Wherefore art thou Challenge [help needed]

Wherefore art thou Challenge [help needed]
0.0 0

#1

Tell us what’s happening:

I can’t seem to pass that last three tests.

Your code so far


function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  arr = collection.filter(val => {
    for(let key in source){
      return source[key] === val[key];
    }    
    return true;
  });
  
  // Only change code above this line
  return arr;
}

whatIsInAName([{ "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/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou/


#2

That for loop inside your filter is not allowing key to get passed the first element property in source. As soon as the return is executed, a value of true or false is returned to the filter. In the following test case, key is equal to ‘apple’ three times (once for each collection element). Sine source[‘apple’] === val[‘apple’] in each element of collection, the filter evaluates to true for each element, so each object in collection is put into the array returned by the filter and then assigned to arr.

whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 }); 

#3

So I need to loop through all the properties in the source and have it passed for every val in the collection?


#4

For each object in the collection array, you should only include it in the final array if the object has ALL the properties and corresponding values of the source object.


#5

Got it! whoaaa! this was crazy…and thank you soo much!