7. Search for a friend, help!


#1


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

Could someone tell me what is wrong with this code?? The error it returns is "Did you create a function called search? Thanks!


#2

Did you mean to write for (var key in friends) {}

And here, did you mean to write,

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

#3

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

Like this it says "it doesn't return info for steve".


#4

Here's the whole code.:slight_smile:

    bill: {
        firstName:"Chris",
        lastName:"Chris2",
        number:"912-555-5555",
        address: ['One Microsoft Way','Redmond','WA','98052']
    },
    steve: {
        firstName:"Seamous",
        lastName:"Seamous2",
        number:"555-666-7777",
        address: ['Ruby Road','Redmond','WA','93542']
    }
};
var list = function(friends){
      for(var key in friends){
         console.log(key);  
    }
};

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

#5

You are not using the provided data. The first names MUST be "Bill" and "Steve". Best to not fool around with lessons, but practice on your own. Stay as close to the instructions as you can.


#6

Thank you so much!!!:smile:


#7

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