7. Search for a Friend


#1

Where's my error? I've tried reading through similar issues but I'm not understanding where my code breaks down. I get an error saying that there is no result for "Steve"

`var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "(206) 555-5555",
        address: ['One Microsoft Way','Redmond','WA','98052']
        },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "(516) 555-5555",
        address: ['Two Apple Loop','Cupertino','CA','11561']
        }
};

var list = function(friends) {
    for (var firstName in friends)
    {
    console.log(firstName);
    }
};

var search = function(name) {
    for(var firstName in friends) {
        if(firstName === name) {
            console.log(friends);
            return friends;
        } else {
            console.log("Not found!");
        }
    };
};`

#2

@designjumper05240,
+++++ nyc object and the for-in loop explained ++++++++++++++++

T h e - B a s i c s

An object has one or more properties seperated by a comma-,
Each property consists of a property-key and it's associated VALUE

var nyc = {
       fullName: "New York City",
       mayor: "Bill de Blasio",
       population: 8000000,
       boroughs: 5
      };
nyc -object- S p e c i f i e d

The nyc object has 4 properties seperated by a comma-,
- a fullName property with property-key fullName and it's associated string value of "New York City"
- a mayor property with property-key mayor and it's associated string value of "Bill de Blasio"
- a population property with property-key population and it's associated number value 8000000
- a boroughs property with property-key boroughs and it's associated number value 5

for - in - loop - e x p l a i n e d

With the for-in-loop you have a Method
which will iterate over all properties of a given object.
At each iteration it will assign the property-key as a string
to a variable name of your choice.
Thus for (var x in nyc)
will lead to 4 iteration's
iteration-1 var x = "fullName";
iteration-2 var x = "mayor";
iteration-3 var x = "population";
iteration-4 var x = "boroughs";
As they want you to display the property-key and NOT it's associated VALUE
you would use
console.log( x );

If they wanted you to display the associated VALUE of the propery-key you would use
console.log( nyc[x] );


#3

@designjumper05240,
The instructions
ask you to give the contacts information of the found friend....

+++ friends list and search and for-in explained.
Look here for an explanation
object
function
for-in-loop
list-function
search-function
http://www.codecademy.com/forum_questions/545275fd8c1ccc8e3e000a23#comment-5457b12c9c4e9d87cb00003f


#4

Thanks for your reply. I'm still not understanding the cause of the error. The instructions explicitly ask to log the associated values to the console ("it should log that friend's contact information to the console and return it"). Regardless, I've updated my function specify the key but I still receive the same error:

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

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


#5

I copied and pasted the search function from the example that you linked, which works, but it logs Bill Gates once, logs Steve Jobs once, and returns Steve Jobs once. Why doesn't it either log AND return Bill Gates or only log OR return Steve Jobs?

Edit: I'm also not explicitly calling the search function. When I call it and pass an argument of "Bill", I get 5 results, Steve Jobs x2, and Bill Gates x3 in the following order: Bill, Steve, Steve, Bill, Bill.

I now have less of an understanding of this exercise than when I initially posed this question.


#6

@designjumper05240,
Please provide us with the =new= code you are using....


#7

Ah! Thank you for your explanation. For some reason I couldn't grasp the error I was making.

I read your post and it was quite helpful - thanks again!