Search for a friend Doesn't Return Steve


I'm having a big problem with the Search for a friend problem.

I have changed the return line repeatedly, and I keep getting the error "It looks like your search function doesn't return contact information for Steve." How do you word this?

var friends = new Object();
friends.bill = {
    firstName: "Bill",
    lastName: "Marshall",
    number: "910-555-0123",
    address: ['One Microsoft Way', 'Redmond', 'WA', '01234']
friends.steve = {
    firstName: "Steve",
    lastName: "Marshall",
    number: "910-555-0124",
    address: ['One Microsoft Road', 'Redmond', 'WA', '01234']
var list = function (friends) {
    for (var key in friends) {

var search = function(name) {
    for (var search in friends) {
        if (friends.firstName === name) {
            return friends[search];


friends is an object. which has two properties: bill and steve, agree?

so here:


friends has no first name property?


Thank you so much! For those who are having the same problem. All I had to change was the line
if (friends.firstName ...) {
if (friends[search].firstName ...) {


do you understand why?


I sure don't. Where was the explanation on why [search] is added to everything?


There is none. The lessons cover how to access the value of an object property, and how to access an element in an array. The name we use for the variable is arbitrary. We can use anything we wish, so long as we are sure there will be no conflicts.

Personally, I never use the name of a function as a variable, even if it won't conflict. It just makes everything confusing for the reader. The name of the function describes what the function does, the name of a variable should describe what it represents.

var search = function (name) {


search is what this function does, name is what it does it with. A reader will see this immediately.

for (var search in friends) {  // is there a better name?


With loops we can think of the iterator variable as representing a singular (one) of the reference object, which is plural (many)

for (var friend in friends) {



Because I used "search" as the key in the for in loop, I needed to pull information from the specific friends object, and using search in brackets designates that I want to pull my information from the object that was active to test the if statement.


It is clear what you were doing, no problem with that once you sorted things out. My concern is the re-use of the same variable name. The function name is semantic (gives meaning) where the variable name is not. I would avoid this practice.


I didn't even see that I had reused it. Wouldn't it make more sense to name it something like searchID or key before naming it friend? From what I can tell it is really more like the arrow that points to the item within the group of things being searched, not actually representative of the friend without the context of the list.


That would be misleading. It is not an ID. It is an index or key.

friend is the singular of friends, so why wouldn't it make sense?

for fruit in fruits

for movie in movies

for animal in animals

for student in students

Bear in mind that the iterator is a locally defined variable and serves some role inside the function. As stated, the name of the function will usually be semantic and describe what the function does. Variables used inside the function should describe what they represent. searchID does not describe what is contained in the object. If it's a friends object, then it will consist of a set of friend objects.

Anyway, not to drag around a dead horse, the main point is to be as descriptive as possible. Arbitrary names gives a very broad brush stroke, which in technial terms has no effect on the running of the code. The intepreter does not care as long as the variable is defined and in the correct scope or context. It's the human readers of your code that we consider when choosing names. Don't let me get under your skin...


wow I was struggling with this for 2 hours . I had the same problem just needed to capitalize the N in firstName ...

Thanks guys !!!
- on to the next lesson!


You're not under my skin. I appreciate the feedback. My hope is to one day collaborate, so understanding naming conventions is actually something I want to accomplish. Not just the mechanics of the coding. Thank you for your input.


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