Tell us what’s happening:
Hi,
I keep reviewing the Basic Code Solution and I can’t get a hang of it…I mean, I understand globally what’s going on, but not the statements in the for loop.
What is this code trying to do ? And why doesn’t it return an array? It returns a boolean value instead?
I don’t get it.
Additionnally I was doing my own code with for loops only, and not using the .filter method and returning an array with the first object and its properties, as asked in the exercice. Then I was stuck and I looked at the Basoc Code Solution and I was stunned.
Is there a way to code this exercise without using th .filter method?
Your code so far
Spoiler - working solution
function whatIsInAName(collection, source) {
// "What's in a name? that which we call a rose
// By any other name would smell as sweet.”
// -- by William Shakespeare, Romeo and Juliet
var srcKeys = Object.keys(source);
// filter the collection
return collection.filter(function (obj) {
for(var i = 0; i < srcKeys.length; i++) {
if(!obj.hasOwnProperty(srcKeys[i]) || obj[srcKeys[i]] !== source[srcKeys[i]]) {
return false;
}
}
return true;
});
}
// test here
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0
.
Link to the challenge:
https://www.freecodecamp.org/challenges/wherefore-art-thou