.7 - Search for a Friend


#1



var friends = {};

friends.bill = {
    firstName: "Bill",
    lastName: "Hates",
    number: "212-666-7734",
    address: ['34 Mega Road', 'Whitetie', 'VA']
    };
friends.steve = {
    firstName: "Steve",
    lastName: "Mobs",
    number: "545-666-7734",
    address: ['13 Hipsta Avenue', 'Yolo', 'CA']
    };
    
var list = function(obj){
    for(var key in friends){
        console.log(key);
    }
};

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

list(friends);
search("Steve");

I'm not sure how to move forward with this. It keeps giving me an error message: "It looks like your search function doesn't return contact information for Steve."


#2

Hi this line

var search = function(obj)

your function should have as argument name instead of obj


#3

It passed me. I still don't really get this exercise all that much anyway.

Why does the console log repeat Steve's contact 2 more times?


#4

I think that is becasue in the functon you have a console.log() and a return statement so that why you have steve print two time

...
console.log(friends[key]);
return friends[key];
...

#5

I removed that part of the code later on and it's still repeating twice.

According to the tutor, it requires me to use the "return" function anyway.


#6

Yes it isn't the code that have the problem but in this lesson and I don't know why it repeat the think you print a ways to see what you print add this line

console.log("==End==")

everithing that will be print after this line is just the lesson that is run it again ..so you will have something like that

bill
steve
{ firstName: 'Steve',
  lastName: 'Mobs',
  number: '545-666-7734',
  address: [ '13 Hipsta Avenue', 'Yolo', 'CA' ] }
==End==
{ firstName: 'Steve',
  lastName: 'Mobs',
  number: '545-666-7734',
  address: [ '13 Hipsta Avenue', 'Yolo', 'CA' ] }
{ firstName: 'Steve',
  lastName: 'Mobs',
  number: '545-666-7734',
  address: [ '13 Hipsta Avenue', 'Yolo', 'CA' ] }
{ firstName: 'Bill',
  lastName: 'Hates',
  number: '212-666-7734',
  address: [ '34 Mega Road', 'Whitetie', 'VA' ] }

you should normally only have this part

bill
steve
{ firstName: 'Steve',
  lastName: 'Mobs',
  number: '545-666-7734',
  address: [ '13 Hipsta Avenue', 'Yolo', 'CA' ] }

But like I already said this lesson repeat the output.


7. Search for a friend. Weird output!?