Wherefore art thou problem

Wherefore art thou problem
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…


#7

I followed the suggestions above, but the code wouldn’t pass all the tests
Then I got the solution with the help from an other post in the forum
Then I created a pure function to make code less convoluted. Here comes just the pure function.

Spoiler
 // a pure function
      let getProperties = (x) => {
        for (let prop in source) {
          //console.log('arr', arr[i][prop]);
          //  console.log('obj ', obj[prop]);
          if (x[prop] !== source[prop]) {
            return false
          }
        }
        return true
      }

#8

Your getProperties function will return a true or false value, so when you write, the following, you are passing the result of calling getProperties with collection and source instead of just passing the function getProperties and allowing the filter to call the function.

getProperties(collection, source)

So instead of:

arr = collection.filter(getProperties(collection, source));

you should be writing it as:

arr = collection.filter(getProperties);

This however does allow you to solve the challenge, because your logic is not correct. But now, you at least have something to work with.


#9

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.