Wherefore art thou Challenge Overcomplicated

Hello campers! This is my first “non-intro” post on the forums! I mostly just wanted to get feedback and possibly help understanding some things.

I have been getting through challenges one by one eventually, and so far haven’t been looking at other peoples solutions. However, when I finished this challenge I thought, “There has to be a more simple way to do this.” So I went to the forums and checked out the solutions to a couple of these challenges that I seem to have over-complicated, and sure enough there is like 2 or 3 lines of code that do the same thing I manage to do in 20+. I have been doing this for almost every challenge.

So the question is: Is there something fundamental I am not getting about Javascript? Or maybe I am just not reading the docs enough, and recreating things that can already be done “natively” in JS?

I already started wracking my brain on this question by comparing my long solutions to better ones, but I want to start being more social on here to learn more from other campers. What do you guys think? Am I even asking the right questions? :laughing:

TL:DR - Why am I over-complicating every challenge?! Example below:

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  var count = 0;
  
  
  collection.forEach(function(obj) {
    count = 0;
    for(var prop in source) {
      if(obj.hasOwnProperty(prop)) {
        count++;
        console.log(Object.keys(source).length);
        if(count == Object.keys(source).length) {
          count = 0;
          var entArr = Object.entries(obj);
          var sourceArr = Object.entries(source);
          entArr.forEach(function(ent) {
            sourceArr.forEach(function(sor) {
              if(ent[0] == sor[0] && ent[1] == sor[1]) {
                count++;
                if(count == Object.keys(source).length)
                  arr.push(obj);
              }
            });         
          });
        }  
      }     
    }          
  });
  console.log(arr);
  
  // Only change code above this line
  return arr;
}

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

It’s normal to find complex solutions for trivial matters in the beginning of your career, don’t worry about it. I think everyone does it for a while, after you get more used to programmin this custom goes away.

not about javascript per se, but about programming. With experience you get to know what a programming lnaguage should have as a basic feature or not, and how you can improve your code using these features.

Again, don’t worry about it, that’s normal and part of the learning process.

1 Like

That actually makes me feel a lot better about it! Practice = experience = better ways. Thanks!! :grin:

That looks like a great resource, thanks for sharing!

Thanks! That seems like a fantastic resource! I will definitely be reading through it. I love to dig deep and really understand everything that is going on (to a practical limit). My brain has been “exploding” every day going through the FCC curriculum! :grinning:

The great thing about FCC is that they often link to MDN references to help you solve the problem. In fact, you already were introduced to Array.prototype.filter() 2 challenges ago, in diff two arrays.

A good way to these challenges is to write down your logic first, then implement the logic with code.

By not writing down actual code, you’re forcing yourself to organize your thoughts by input and desired outcome. You can get a more clear picture of how to solve the problem this way

1 Like

Seems like I am not using all the resources they are putting right in front of my face. Thanks! I will be checking and reading hints a lot closer.