7. search for a friend -- please help!


#1


https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/7?curriculum_id=506324b3a7dffd00020bf661#

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?
Thanks!

function list(friends)  {
    for (firstName in Friends) ;
 console.log(list);
};

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

#2

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)

#3

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.

console.log(key);

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


#4

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