Intermediate Algo Scripting: Wherefore Art Thou Own Solution Not Happy

Tell us what’s happening:
Disclaimer: I haven’t seen FCC’s solution for this challenge. Just figured out a way to pass this challenge. As you can see I’ve manually written down the source keys index seeing that the challenge required us to pass in an object which had one or at the most two properties. But what if the source obj we’re passing in has more than 2 properties. How do we figure that out? Also, am I hardcoding this stuff?

   **Your code so far**

function whatIsInAName(collection, source) {
     
var newArr = [];

var sourceKeys = Object.keys(source);
console.log(sourceKeys);

if(sourceKeys.length <= 1) {
   collection.forEach(currentObj => {
       if(currentObj.hasOwnProperty(sourceKeys[0])) {
           if(currentObj[sourceKeys[0]] === source[sourceKeys[0]] ){
               newArr.push(currentObj);
           }
       }
   })
}

if(sourceKeys.length > 1) {
   collection.forEach(currentObj => {
    if(currentObj.hasOwnProperty(sourceKeys[0])){
        if(currentObj.hasOwnProperty(sourceKeys[1])){
            if(currentObj[sourceKeys[0]] === source[sourceKeys[0]]){
                if(currentObj[sourceKeys[1]] === source[sourceKeys[1]]) {
                    newArr.push(currentObj);
                }
            }
        }
    }
 });

}

return newArr;

}

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

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.192 Safari/537.36.

Challenge: Wherefore art thou

Link to the challenge:

you have the sourceKeys array, and you have a condition that must be true for every element of that - do you know how to verify that a condition is true for every element of an array?

I have no clue what you just said . . .

First property collection that is given to you looks like this:
[ {}, {}, {}… ]
It is an array of objects, where each object is:
{key:value, key:value, key:value…}.

Second property, source is just an object:
{key: value, key:value, key:value…}

Your task is to find objects in collection that have the key:value pairs written in source (they can have extra pairs and that will be ok).
Then put them into an array and return them.

Logic of this is:

for everyObjectInCollection:
  checkObject():
    if (itHasKeyValuesThatMatchesSourceObject()):
      putItIntoArrayResults()
returnResults()
1 Like