Hi, I’ve been struggling with the Where for art thou problem for a while.
This is what I’ve got so far:
function whatIsInAName(collection, source) {
var arr =[];
keys = Object.keys(source);
function check(item){
if (item[keys] === source[keys]){
return true
}
return false
}
arr = collection.filter(check)
return arr
// Only change code above this line
}
It passes the first two tests, but not the last two. Could someone point me in the right direction?
function whatIsInAName(collection, source) {
var arr =[];
var keys = Object.keys(source);
console.log(keys.length)
function check(item){
for (var i = 0; i < keys.length; i++){
if (item.hasOwnProperty(keys[i]) && item[keys[i]] === source[keys[i]]){
return true
}
else return false
}
}
arr = collection.filter(check)
return arr;
// Only change code above this line
}
whatIsInAName([{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }], { "a": 1, "b": 2 })
Thanks for posting! I was also in the middle of struggling with this and your post helped me think through it. I did a slightly different take that also worked. I assume the code in the second post worked?
function whatIsInAName(collection, source) {
// What's in a name?
function compare(prop) {
return collection[i][prop] === source[prop];
}
var arr = [];
var sourceProps = Object.getOwnPropertyNames(source);
for (var i = 0; i < collection.length; i++) {
if (sourceProps.every(compare) === true) {
arr.push(collection[i]);
}
}
return arr;
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
Thanks!!!