7. Search for a friend


#1


I've been onto 4 other threads for this same exercise, my code is identical to everyone else's, but I get "Oops, try again. It looks like your search function doesn't return contact information for Steve."

Is it because I've been creative and gone for Chandler, Joey and Ross rather than Bill and Steve? Thanks


var friends = 
{
    chandler: 
    {
        firstName: "Chandler",
        lastName: "Bing",
        number: "12345678",
        address: ['Flat 15', 'New York City', 'USA']
    },
    joey: 
    {
        firstName: "Joey",
        lastName: "Tribbiani",
        number: "87654321",
        address: ['Flat 15', 'New York City', 'USA']
    },
    ross: 
    {
        firstName: "Ross",
        lastName: "Geller",
        number: "01010101",
        address: ['Apartment 23', 'New York City', 'USA']
    }
};

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];
        } else { 
            return "Invalid";
        }
    }
};


#2

Although a absolutely love your the friends reference, I think you may have to use the names they tell you to use.


#3

Ahh, rubbish, was just trying to mix things up a bit! And not do everything word for word. Ok never mind, will edit, thanks!

Edit: I've just replaced them with Bill and Steve, still no luck, still getting the same "Oops, try again. It looks like your search function doesn't return contact information for Steve." error message.

Edit 2: I've just edited the function on the end, where return is now "return steve[key];
Nothing happens but I get the green Way to Go next lesson message? So confused!

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

#4

I much prefer your version

I'm not sure the phone numbers should be strings.

And also I don't think this should be in your code.

Let me know if that solved it.


#5

you wrote return steve[key]
It should be friends[key]


#6

I have been having an issue with the same problem, I believe that the page has bugs, try this code and it works which I know it is wrong for sure

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


#7

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