6. list em' all. Need help understanding please


#1


6.list em all - contact list.


Can someone explain to me how this works? I don't understand how the function gets any info from the object. The parameter you put into the function doesn't seem to matter, as long as it matches the name of the object in the function, and also the key doesn't matter as long as it matches what is in the console.log.

I don't see how the function can pull any data from the friends object when nothing in the function or the calling of the function refers to it. Any help? While I can pass this section I would like to understand it. Thanks


var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "555-555-5555",
        address: ["One Microsoft Way", "Redmond", "WA"]
        },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: " 760-555-5555",
        address: ["One Infinite Loop", "Frisco", "CA"]
        }
}

var list = function(matches){
    for(var anything in matches){
      console.log(anything);        
    }
}
list();


#2

functions have access to anything that is global, your friends object is global, as this code proves:

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "555-555-5555",
        address: ["One Microsoft Way", "Redmond", "WA"]
        },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: " 760-555-5555",
        address: ["One Infinite Loop", "Frisco", "CA"]
        }
}
var list = function(){
   console.log(friends);
}
list();

you pass the object as argument, this will also work:

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "555-555-5555",
        address: ["One Microsoft Way", "Redmond", "WA"]
        },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: " 760-555-5555",
        address: ["One Infinite Loop", "Frisco", "CA"]
        }
}
var list = function(x){
   console.log(x);
}
list(friends);

both approaches work, the advantage of the second approach is that you can use this function for a different object by passing the object as argument on function call


#3

Thanks for the quick reply! And thanks for explaining that functions can access global things.

I noticed in your code you refer to the friends global object at least in some way. In the first example you log the friends variable, and in the second you pass friends as a parameter while calling the function, but in my case I'm still able to pull information from the friends object even though I never refer to it. It's not referred to inside the function itself, nor is it referred to while calling the function. My code as it is has been returning bill and steve ( that completes the assignment, but I don't see how it can get information from the friends object when my function doesn't ask for any information from the friends object).

Say I have 2 global objects, how does the function know which one to pull?

Sorry if I'm just completely missing the point. I'm trying to get these done as an entrance test prep and it's hard trying to move on with out understanding this. Thanks

Here's my exact code as it is now:

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "555-555-5555",
address: ["One Microsoft Way", "Redmond", "WA"]
},
steve: {
firstName: "Steve",
lastName: "Jobs",
number: " 760-555-5555",
address: ["One Infinite Loop", "Frisco", "CA"]
}
}

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

//this returns 'bill' and 'steve'


#4

i was hoping i didn't have to go there, this is caused by the codecademy exercise validation. i put your code in a bin, if you run it nothing will be logged

If you however pass friends as argument on the function call, your code does work


#5

Oh, so it's just due to the way the exercise handles verifying my answer? I was tearing my hair out wondering what I wasn't understanding haha. I went back and did all the previous JavaScript exercises just to make sure I understood everything correctly.

So normally this code wouldn't log anything unless I pass friends to the function call?


#6

that is one of the two ways, you can also loop over friends since function have access to global objects.

You can also try this in the bin


#7

Awesome, thank you so much! Can't tell you how much I appreciate it! +10


#8

you're welcome :slight_smile:


#9

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