Tell us what’s happening:
Hi, here I have chosen to loop through the collection, and find the items that have the same properties as source does, before comparing the values of the properties, and push it into the arr if they are the same. I’m not sure what went wrong here. the returned arr contains only the property pairs that matched, instead of the collection item that contains the matched pairs.
Can someone help me? thanks
Your code so far
function whatIsInAName(collection, source) {
// What's in a name?
let arr = [];
// Only change code below this line
for(let i in source){
for(let j = 0; j < collection.length; j++){
if(collection[j].hasOwnProperty(i)){
if(collection[j][i]===source[i]){
arr.push(collection[j])
}
}
}
}
console.log(arr)
// Only change code above this line
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou
