7. Search for a friend :P


#1

var friends = {};

friends.bill = {
firstName: "Bill",
lastName: "Showers",
number: "303-678-9221",
address: ["Castle Rock", "CO"]
};

friends["steve"] = {};

friends.steve["firstName"] = "Steve";

friends.steve["lastName"] = "Garrets";

friends.steve["number"] = "3038907814";

friends.steve["address"] = ["Tiger", "Army"];

var list = function(x){
for (var key in x)
{
console.log(key)
}
}

//Catonyourhead here! here's what i'm stuck on... I thought I understood... guess not!
//I would love to hear your reasoning as to why this code prints undefined, rather than the information at the coordinates I thought I had provided. Can't you use the passed in variable as a coordinate(key)? or did I do something wrong? Also, if you think I'm unclear on anything else, I'm very willing to learn!
//Thanks!!

var search = function(name){
for (var x in friends){
if (x === name.toLowerCase()){
console.log(friends.x)
}
}
}


#2

You're accessing the attribute x of friends.. and it has no such attribute, it's undefined

You do have a variable called x in that scope though, perhaps use that instead.


#3

isn't x my local variable for substitution of each item in my list? and if so, wouldn't using friends.x turn into friends.(whichever name is being used) ? Please help. I guess I am unclear.


#4

Whenever you put a dot . after something, you're accessing something in that scope, in this case the scope of friends. And in that scope, there is no x.

Use friends[x] instead, which is lookup by value.