7. Search For A Friend - Stuck


#1

I'm stuck on 7. Search For A Friend receiving the error 'Oops, try again. It looks like your search function doesn't return contact information for Steve.' but I'm not sure what I am missing. Can anyone tell? Thank you for all of your help!


var friends = new Object();

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Whillup",
        number: "314-455-2322",
        address: ["One Sunset Blvd"]
    },
    steve: {
        firstName: "Steve",
        lastName: "Jordan",
        number: "808-332-4443",
        address: ["One Vegas Court"]
    }
};    

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

list(friends);

var search = function(name){
    for (var firstName in friends){
        if(friends.firstName === name) {
            return (friends.firstName, lastName, number, address);
        }
    }
};


#2

Hi
I noticed one major problem and one minor problem in the function search :

1) major problem : in your if(friends.firstName === name) {, you should specify which element of friends has to be checked. You should rename the variable firstName to key to have something like

var search = function(name){
    for (var key in friends){
        if(friends[key].firstName === name) {   # don't forget the [key] to specify 
 # which element is being checked

2) minor problem : I don't really know what is expected as output of the function, I would all simply return friends[key];

Cheerio


#5

Awesome, thank you so much @thebelgian1 - I had no idea I had to actually use [key] inside the syntax. I feel like I am so close now, but I'm still getting an error of "Oops, try again. Did you create a function called search?"

Here's the updated code if anyone can see anything glaring:
var search = function(name){
for (var firstName in friends){
if(friends[key].firstName === name) {
return friends[key];
}
}
};


#6

Hi this part

if(friends[key].firstName === name)

Instead of ** friends[key].firstName** You should pur friends[firstName].firstName since You used firstName in the for in .
Also here

return friends[key];

should be

return friends[firstName];

Hope that could help you.


#8

Thank you wizmarco!!


#9

This topic was automatically closed after 24 hours. New replies are no longer allowed.