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: