[7] search function implementation issue


#1

Hello everybody,

I cannot figure out what is wrong with my code...
Some help would be greatly appreciated :smile:

Here is the error message:
"Oops, try again. It looks like your search function doesn't return contact information for Steve."

Here is the code:

var friends = {
    bill: {
        firstName: "bill",
        lastName: "Doe",
        number: "(0101) 555 5555",
        address: ["34 Regent Garden", "New San Angeles", "Planet Mars"]
    },
    steve: {
        firstName: "steve",
        lastName: "Smith",
        number: "(0101) 555 5554",
        address: ["31 Regent Garden", "New San Angeles", "Planet Mars"]
    }
};
var search = function(name) {
    for (var f in friends) {
        if (friends[f].firstName === name) {
            console.log(friends[f]);
            return friends[f];
        }
    }
};
console.log(search("steve")); // statement added for debugging purposes

And finally here is the output of my code:

{ firstName: 'steve',
lastName: 'Smith',
number: '(0101) 555 5554',
address: [ '31 Regent Garden', 'New San Angeles', 'Planet Mars' ] }
{ firstName: 'steve',
lastName: 'Smith',
number: '(0101) 555 5554',
address: [ '31 Regent Garden', 'New San Angeles', 'Planet Mars' ] }

Regarding the output:
I am guessing the first entry is the output of the console.log statement inside the for/in loop,
and the second entry is the same information but this time returned by the search function.

I must be doing something "stupid" somewhere but I fail to see what...

Any ideas ? :smile:

P.S. I added the last statement so I could verify that the search function prints the contact information for steve and then returns that same contact information. I get the same error message with or without that last statement.


#2

I just copied your code and changed the names for Steve and Bill (upcase) and it worked :smiley:

Please check that out :smile:


#3

Thanks A Lot.

Stupid Stupid Stupid mistake :smile:

Once again Thanks :smile: