Wherefore art thou problem

Wherefore art thou problem
0.0 0

#1

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:


#2

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


#3

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” });


#4

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


#5

thanx for the “collection” thing…