7. Searching for Friends!


#1

Hey friends :grinning:

The coding is allowing me pass to the next section, but the output is:

undefined
undefined
undefined

Any tips as to why I’m not producing the desired output?

var friends = {
bill: {
firstName: "Bill",
lastName: "Brown",
number: "(206) 555-5555",
address: ["One Microsoft Way", "Redman", "VA", "33333"]
},
steve: {
firstName: "Steve",
lastName: "Steverson",
number: "(456) 468-9852",
address: ["Twenty Two", "Beaverton", "OH", "444433"]
}
};
var list = function (friends){
for (var firstName in friends) {
console.log(firstName);
}
}; 
var search = function(name) {
for (var key in friends) {
if (friends[key].firstName === name) {
console.log(friends.firstName);
return friends[key];
}
}
};

#2

for the first console.log you are looping through friends and assigning each one to firstName, therefore firstName is actually the whole bill or steve object, not the firstName attribute of each one. so that should look more like your second loop.
for(var key in friends){
console.log(friends[key].firstName);
}

for the second console.log its because there is no object ‘firstName’ in friends you should have console.log(friends[key].firstName) ;


#3

Thanks doggo! That helped a lot!


#4

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