Contact list 6/8: Oops, try again. It looks like your list function isn't printing the right output. What do I do?


#1

Here's my code:
var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "555-555-5555",
address: ['One Microsoft Way', 'Oxford', 'OX69 0MW']
},
steve: {
firstName: "Steve",
lastName: "Backshall",
number: "525-555-5555",
address: ['One Jungle Way', 'Snake', 'SN99 0JW']
},
};
var list = function(object) {
for (var friends in friends) {
console.log(friends);
}
};
It tells me that my list function isn't printing the right output. I don't know what i'm doing, if someone could come back to me as soon as possible that would me greatly appreciated :smile:


#2

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "(323) 233-4345",
address: ['One Microsoft Way','Redmond','WA','98052']
},
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "(323) 234-4535",
address: ['One Microsoft Way','Redmond','WA','98052']
}
};

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

The object in the function "list" should be "friends" you have wrote "object"
just change it and your good to go!

I hope this was helpful!! :grinning:


#3

This solved my issue too, but I don't know WHY it worked out this way.


#4

I have the same question as @leecalvink. Can anyone explain the list function and especially the 'friends' in that function?


#5

I think the confusion here arises because the function in this example has friends as a placeholder keyword:

var list = function(friends) {
  for (var -->friends<-- in friends) {
    console.log(-->friends<--)
  }
}

While also using friends as a parameter:

var list = function(-->friends<--) {
  for (var friends in friends) {
    console.log(friends)
  }
}

It could have easily been done like so, and is less confusing:

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

The list function is taking in the object friends and "looping" over each property (represented by the x) in that object and logging it to the console. It makes more sense to me now that I've had more practise at this whole business.


#6

Agreed - it really is more confusing than it needs to be! The suggested "key" and "In object" had me stumped for a while.