Search for a friend


#1
var friends = {
Bill:{
    firstName: 'Bill',
    lastName: 'Clinton',  
    number: 123456,
    address: ['Potato street', 'Motherwell']
    },
Steve:{
    firstName: 'Steve',
    lastName: 'Nash',  
    number: 654321,
    address: ['Cucumber ave', 'Motherwell']
    },
Bob:{
    firstName: 'Bob',
    lastName: 'BBQ',
    number: 112233,
    address: ['qwerty street', 'Hammilton']
    },

};

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

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

> list(friends);
> search('Bob');

output:

Bill
Steve
Bob
"not found"

Why search function doesn't work?


#2

@cecop,
In your search function you are using the return statement
like

return "not found";

this causes an immediate EXIT of the function.....
and for "Bob" to be found....
you first have a "Bill" --> return "not found";