List'em all!


#1

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) {
console.log(prop);

    }

}

#2

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) {
        console.log(list)
   }
}

I hope that I could help you.


#3

Thanks I understand now :grinning:


#4

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??


#5

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.
...

#6

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.


#7

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


#8

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.