Tell us what’s happening:
it is showing that source.key[i] is undefined
Your code so far
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
// Only change code below this line
key=Object.keys(source);
collection.filter(function(val){
for(i=0;i<key.length;i++)
if(val.hasOwnProperty(source.key[i]))
return val;
});
// Only change code above this line
return collection;
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36
.
Link to the challenge:
https://www.freecodecamp.org/challenges/wherefore-art-thou
You’re perhaps missing a var
or let
in front of your first use of i
, though that’s not the problem
Did you perhaps intend to use source[key[i]]
?
For what it’s worth I think it’d be better to use a for(var key in source)
here, or indeed anywhere that similarly loops over an index just to immediately use that index in the collection again
its still not working
function whatIsInAName(collection, source) {
// What’s in a name?
var arr = [];
// Only change code below this line
key=Object.keys(source);
collection.filter(function(val){
for(i=0;i<key.length;i++)
if(val.hasOwnProperty(key[i]) && val[key[i]]==source[key[i]])
return val;
});
// Only change code above this line
return collection;
}
whatIsInAName([{ first: “Romeo”, last: “Montague” }, { first: “Mercutio”, last: null }, { first: “Tybalt”, last: “Capulet” }], { last: “Capulet” });
key
isn’t defined anywhere, it’s not a global and you’ve not declared it anywhere here, same with i
- consider putting var
or let
in front of the first time you use them.
filter
doesn’t modify collection
, it just returns a new collection which you haven’t saved anywhere
thanx for the “collection” thing…
Thanks again for your help.
I got it.
I just used one parameter in the getProperties function, which is for the filter function
to work with. Then the rest is the same.
In the above code, now stands just the pure function.