List'em all!


Can someone explain me how this works pleaseee ????

var friends = {

bill: {firstName:"Bill",lastName:"haha",number:"15" , address: ['no','yes']},
steve:{firstName:"Steve",lastName:"hahaaks",number:"25", address: ['nojo','yesckh']}


var list = function (object) {
for (var prop in object) {




Hi in this lesson they ask you to use a for loops to print all the key and they aske you to create a function list

first writte a function list that takes a single parameter in our case friends

var list = function(friends)

and then we use the for loops like the one they put on top of the instruction so its becomes

var list = function(friends) {
   for (var list in friends) {

I hope that I could help you.


Thanks I understand now :grinning:


Hey, why do you define a new variable in the for loop with the same name as the function?
Won't there be two variables with the same name??


No there wont. But when you have plural in for (or foreach, for that matter) loop, like friends, try to use singular, like friend, for one results.

for (var friend in friends) ...
for (var companion in companions)...
for (var car in cars)... 
// etc.


Hi there is no reason why I used it and I've never notice it! but I think that its diferente for when you go throught an Object ...because when you go thought an array and you use the same name of the function its won't works.


Thanks, that's a useful hint, just by changing that in the code I was writing it became more obvious!


Ok, it just seemed odd since you define the function and then call the supposed function inside itself. But I got this exercise already :wink: Thanks for the answer.