Wherefore art thou :(

Tell us what’s happening:
I have a feeling that something is wrong with my filtering part, as I cant return empty value upon finding no match!
Your code so far


function whatIsInAName(collection, source) {
    // What's in a name?
    var arr = [];
    // Only change code below this line
     var obj=[];
      for( var i= 0;i<collection.length;i++){
        obj=Object.keys(collection[i])
             }
   
     console.log(obj);
     
           
  for(var i=0;i<obj.length;i++) {
   if(source.hasOwnProperty(obj[i])){       
    arr= collection.filter((item)=>{  if(item[obj[i]]===(source[obj[i]]))
   {return item[obj[i]];}
  
    })
   }

       
          }
    // Only change code above this line

    return arr;
  }
  

Your browser information:

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

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

Hi @ShaheenHuzir,

If you look on the documentation for Array.filter, you’ll see that the function that filter is expecting is what is called a predicate function.

Unfortunately, this particular lesson can be a tricky one, and there are several things you will need to change to get all the tests to pass correctly. The fact that you are currently passing some of the tests is by luck due to the way you’ve written your logic… I only know this because I wrote a lot of “lucky code” when I was first learning JS and my hubby (who is already a full stack dev) would have to help me understand why my code was lucky rather than sound.

I would actually recommend starting by grabbing the source keys rather than the collection keys.

Here are the steps I would take to solve this one:

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  
  // Only change code below this line



  // store the source keys that exist on the source obj

  // loop over the collection to filter it
  // hint: this can be with .filter or a standard loop (but using .filter will make writing the next bit of code correctly much easier)
  
    // store the keys for the current item in the collection
    // loop over the source keys
    // hint: a standard for loop here would suffice

      // if the item does not have the current source key OR
      // it does have the key but the the value of the items key and the value of the source key are not equal, don't include it in your return array
      // hint: not including the item would require you to return false if you decided to use .filter
  
    // if you were able to loop over all the source keys and you have not decided against keeping the item, include the item in your return array
    // hint: including the item would require you to return true if you decided to use .filter


  // Only change code above this line
  return arr;
}


One example of a logic issue with your current code:
(namely the way you are gathering the collection keys).

var obj=[];
      for( var i= 0;i<collection.length;i++){
        obj=Object.keys(collection[i])
             }

This bit of code is replacing the obj array every time it iterates, so you are really only retaining the keys of the last item in the collection (which could be a problem if the last item in each collection didn’t contain all the keys of the ones before it). You could test this by changing the collection to include a different key name like this (I changed the ‘last’ to ‘middle’ in the first item of the collection:

[{ first: "Romeo", middle: "Montague" }, 
               { first: "Mercutio", last: null }, 
               { first: "Tybalt", last: "Capulet" }], 

Your current method for collecting the keys would still return only first,last and would overwrite the collecting of the ‘middle’ key due to your assignment (=) operator vs using something like .push() Not to say you should move forward with your current code the way it is and simply add .push because that still won’t solve the issue (and .push is not even required to pass this lesson).

2 Likes

Thank you so much for the insight!! Oh yes it is true that I am shooting blind folded hoping to hit the target. This part has got me asking questions to myself if I can make it or not! :confounded:

2 Likes

Oh yes it is true that I am shooting blind folded hoping to hit the target. This part has got me asking questions to myself if I can make it or not!

Hey now, you already sound like a coder! :stuck_out_tongue_winking_eye:

But in all seriousness, if you want it, you can get there.

One thing to keep in mind is that the Intermediate Algorithm Scripting section is a real tough one and was genuinely designed to be a challenge. Back when I was going through this section I remember taking lots of breaks (sometimes months long) due to how frustrated I would get and how stupid I felt. My husband would look over my shoulder sometimes and tell me that most of day to day coding does not look like this section at all. So while that is great news, it still a great brain exercise that will show you a lot of potential pitfalls a coder can run into. Expect to make lots of mistakes throughout this particular section, expect for each lesson to potentially take hours/days (not all will).

It’s very normal to feel the way you are right now. And while I want to say it does get better, I still have days where I feel the way you do. I will say the things that I used to get stuck on for days I am able to push through much faster. So don’t give up, the best feeling is when you finally get to solve a challenge in what feels like a really creative way using all that you have learned.

…and here’s a little secret, I was so down on myself about my struggles with this particular section, that I literally started the entire Javascript Algorithms And Data Structures Certification section over and went back through every lesson and re-did all of them a second time because I figured I must not have really learned it the right way the first time through. While I don’t know that I would recommend doing what I did, I will say it taught me that I had actually been learning the content and was doing much better than I thought was. The second time through everything was sooo much easier.

1 Like