type or paste code here
function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line
var keyarr=[];
keyarr=Object.keys(source);
var collectionarr=[];
for(let i=0;i<collection.length;i++){
collectionarr[i]=Object.keys(collection[i]);
}
console.log(source[keyarr[0]]);//verifying whether this type of assumption works
console.log(collection[2][collectionarr[0][0]]);//verifying whether this type of assumption works
for(let i=0;i<collection.length;i++){ //looping to iterate through each and every object from the collection
for(let j=0;j<collectionarr[i].length;j++){//looping to iterate through properties of each object of the collection
for(let k=0;k<keyarr.length;k++){//looping to iterate through all second argument properties
if(source[keyarr[k]]===collection[i][collectionarr[j][k]]){//comparing second argument value with the collection's value
arr.push(collection[i]);
k=keyarr.length; //in order to go out of the loop, as the object is added already and we don't want to add this object in our array if the condition satisfies for any other property...
j=collectionarr[i].length;//we need to go the collection [i], so increamenting things for that
}
}
}
}
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" });
Why doesn’t my code works… it is not passing my if condition… why am I unable to compare? please help. ( I started from C, so you can see C type of solving Javascript)