Search for a friend Doesn't Return Steve


#1

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) {
        console.log(key);
    }
}

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

#2

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

so here:

friends.firstName

friends has no first name property?


#3

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


#4

do you understand why?


#5

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


#6

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 for..in 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) {

}

#7

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.


#8

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.


#9

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.


#10

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...


#11

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!


#12

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.


#13

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