7. search for a friend -- please help!



Hi everyone, this is driving me crazy. I've tried many times to get it right and I still get asked the same question:
"Oops, try again. Did you create a function called search?"

As far as I know, I did. Many times now, but I still get this message.
Do you see what I'm doing wrong?

function list(friends)  {
    for (firstName in Friends) ;

var search = function(name)  {
    for(var firstName in Friends) {
        if(friends[key].firstName == name) {
            return friends[key];


well, first you have problems in list function:

for (firstName in Friends) ;

that is not a great for loop, for loops have {} for the function content:

for (firstName in Friends) {
  // function content

and you are going to log list (the function) to the console? Not sure that is wise.

in your search function, where does key from? You called your key firstName:

for(var firstName in Friends)


The friends object is global, so can be seen inside the function. Consequently we don't need a parameter.

var list = function () {


The object cannot be referred with a different name, Friends is undefined. We should not use capitalization of variable names. That is reserved for classes. Use the lowercase name given the original object.

It's also a good idea to avoid using properties as variable names. Stick to something generic that describes its purpose in the loop.

for (var key in friends) {


Note the syntax.

Inside the loop, simply log the key value.


Follow the above advice to clean up your search function, as well, then repost your code if you are still having problems.


Thanks @mtf and @stetim94, that's all very helpful. I've revised the code and passed the lesson.