Oops "doesn't return contact information for Steve" AND "did you created a function called search"


#1

I'm completely stuck at "7. Search for a friend"
I've tried to change what I wrote inside search function.

When I write it like this, I got the message error "It looks like your search function doesn't return contact information for Steve."

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

but when I try to use Steve's name inside the function, like this, I get the error "Did you create a function called search?"

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

for god's sake, help me, I have no clue what's wrong.


#2

The parameter should be name. For this function, friends is accessed globally.


#3

I've changed the parameter as you suggested, but I'm still getting the message "Did you create a function called search?"


#4

Looking at your first example from above...

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

#5

ooh thaaanks a lot man
it was that little .firstName that was missing
I didn't get that for/ in logic very well, so it was kinda hard to find my mistakes.
but now it works!


#6

The in operator tracks down all the keys of an object (in no particular order), but you probably figured that out by now.

for (key in object) {
    console.log(key, object[key]);
}

#7

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