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?
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" });