I am working on Wherefore Art Thou in the Javascript Intermediate Algorithm Scripting section. I have been stuck on this one for several days. I’ve looked up other answers, watched videos, etc. but I can’t seem to figure out why my code isn’t working. I can only pass the first couple tests. Now it seems to stop running after the first true result and doesn’t increment properly through the rest of the collection array. Help!
function whatIsInAName(collection, source) {
// Only change code below this line
var sourceKeys = Object.keys(source);
//console.log(sourceKeys);
var resultArr = collection.filter(function(obj){
//i is not incrementing. Why?
for(let i = 0; i < collection.length; i++){
console.log("sourceKeys[i]: " + sourceKeys[i]);
//console.log(source[sourceKeys]);
//console.log(obj[sourceKeys]);
console.log("obj[sourceKeys[i]]: "+ obj[sourceKeys[i]]);
console.log("source[sourceKeys[i]]: " + source[sourceKeys[i]]);
console.log(i);
if(!obj.hasOwnProperty(sourceKeys[i]) || obj[sourceKeys[i]] !== source[sourceKeys[i]]){
console.log(false);
} else{
console.log(true);
return obj;
}
}
});
return resultArr;
// Only change code above this line
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
Much appreciated.