Contact List: Search for a friend...Whats wrong with my code


#1


I am getting an error message: Your search function do not return contact information for steve...
var friends = {};
friends.bill = {};
friends.steve = {};
friends.bill.firstName = "bill";
friends.bill.lastName = "rodes";
friends.bill.number = 4143433333;
friends.bill.address = ["194 W Way", "Del St.", "92343"];
friends.steve.firstName = "steve";
friends.steve.lastName = "Smith";
friends.steve.number = 5453332121;
friends.steve.address = ["222 S Hay", "Sea Ct.", "92545"];
var list = function (obj)
{
    for(var key in friends)
    {
        console.log(key);
    }
};
var search = function (chk)
{
    for(var i in friends)
    {
        if(friends[i].firstName === chk)
        {
            return friends[i];
        }
        else
        {
            return "No results found";
        }
    }
};
list(friends);
search("bill");


#2

a function ends the moment a return keyword is reached, this currently happens in the first iteration of your loop. you only want to return not found after the loop has finished running


#3

You do not need need an else statement,

Just inlcude the return statement right after your console.log and then close your if statement and function. :slight_smile:

And if the Firstname is found in the search you are to console.log friends[i] not return it,

console.log(friends[i]);


#4

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