Search for a friend


#1


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


There is no error ,
I just want to ask why we use object name 'friend' in search function,
because if there is any other object , not friend,
this search function will not work.


Replace this line with your code. 

var friends = {};
    friends.bill = {
        firstName: 'Bill',
        lastName: 'Gates',
        number: '91-233432211',
        address: ['One Microsoft Way','Redmond','WA','98052']
    };
    friends.steve = {
        firstName: 'Steve',
        lastName: 'Jobs',
        number: '91-233432254',
        address: ['infinte loop','kapoor','Wg','99052']
    };
    friends.rahul = {
        firstName: 'Rahul',
        lastName: 'Dubey',
        number: '91-233432216',
        address: ['definte ','koor','Wm','98652']
    };
    var list = function(name) {
        for (var key in name) {
            console.log(key);
        }
    };
    var search = function(name){
        for (var key in friends) {
        if(friends[key].firstName === name){
            console.log(friends[key]);
            return friends[key];
        }
        }
    };
    search('Steve')


#2

That's true. It would be better to make the search function part of the friends object and the "friends"-array as an attribute of the friends object. Good thinking! :slight_smile:


#3

so how we do that ?---------------------


#4

Like this:

friends.search = function(name) {

}

But the friends object has to be in an object of friends:

friends.friens = {};
friends.friens.bill = {
  name: "bill"
}

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.