7. Search for a Friend-HELP!


#1


Oops, try again. It looks like your search function doesn't return contact information for Steve.


var friends = {}
    friends['bill'] = {}
    friends.steve = {}
    friends.mai = {}
    
    var friends = {
        bill: {
            firstName: "Bill",
            lastName: "Gates",
            number: "(206) 555-5555",
            address: ['One Microsoft Way','Redmond','WA','98052'] 
        }
    };
    
    var friends = {
        steve: {
            firstName: "Steve",
            lastName: "Jobs",
            number: "(415) 290-5678",
            address: ['One Infinite Loop Drive','Mountain View','CA','94039']
        }
    };
    
    var friends = {
        mai: {
            firstName: "Mai",
            lastName: "Nguyen",
            number: "(909) 456-1234",
            address: ['570 6th Avenue','San Francisco','CA','94118']
        }
    };
    
    var list = function (friends) {
        for (var key in friends) {
            console.log(key);
        }
    }
    var search = function (name) {
    for (var key in friends)
    {
        if(friends[key].firstName === name) {
            console.log(friends[key]);
            return friends[key];
        }
    };
}
console.log(search("Steve"));


#2

You are replacing the variable friends with an object...the only searchable name is Mai.
Did you mean to declare the properties for each friend object you created at the beginning instead? If so, delete the duplicate var friends declarations, and define the properties per object instead.

friends.object = { property1: value1, property2: value2, propertyN: valueN};

Example:

friends.bill = { firstName: "Bill",
            lastName: "Gates",
            number: "(206) 555-5555",
            address: ['One Microsoft Way','Redmond','WA','98052'] };

Also, you're printing the found friend twice, once in your search function, and once during your function call at the end, console.log(search(name)). Remove one of the two (the one on the search function, perhaps).


#3

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