I do not understand why this works (objects, for/in)


#1

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/6?curriculum_id=506324b3a7dffd00020bf661#

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
The code is working but I don’t understand it. I see no relationship between my function and the object it is searching. It pulls the names from the object successfully but I have no idea how it even knows what object I want it to search. Can someone explain this?

```

var friends = {
bill: {
firstName: “Bill”,
lastName: “Stevens”,
number: “212-885-7676”,
address: [“1072”, “Wyoming Ln”, “Wisconsin”, “OH”, “32324”]
},
steve: {
firstName: “Steve”,
lastName: “Ryerson”,
number: “330-876-9898”,
address: [“1212”, “Peeo Dr.”, “Albequerce”, “NM”, “21345”]

},
andrea: {
firstName: “Andrea”,
lastName: “Hobert”,
number: “666-353.3532”,
address: [“234”, “Bridgestone Dr.”, “Cincinatti”, “OH”, “44240”]
}
};

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

<do not remove the three backticks above>

#2

adding a function call might help you understand:

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

so when we call the function, we supply an argument to satisfy the function parameter. so jarjar will now contain the friends dictionary, which you then loop over

does that help? Its always tricky to remember that until functions are called, function parameters are just placeholders


#3

I’m still not understanding. I do get that a functions argument is just a placeholder. But I still see no relationship between my function and the object above it. In the example you provided for clarity of calling the function we are referencing my objects variable name (i.e. “friends”) In the function that searches the object there are no words referencing my object at all. Like what if I had 4 objects? How would it know which one I was trying to search?


#4

how is this:

for (var friend in friends)

any different then;

var list = function (jarjar){
    for (var friend in jarjar)
}
list(friends);

its basically the same, except the function could work for multiply objects by simply supplying a different argument at function call.

friends (the argument) gets passed to the function parameter (jarjar), so by looping over jarjar, you are looping over friends, just indirectly.


#5

Without calling the function I haven’t entered an argument to be passed to the parameter though. And yet the function still works. It still console logs the three names I have listed in my object.


#6

true, because the function doesn’t execute unless called, until the function is called the parameter is a placeholder

Yea, the exercise validation calls your function for validation purpose, this causes output.


#7

Thanks for explaining that but that is really confusing for someone still trying to learn these things. I wish they would just have had a bit of code automatically entered at the end of that exercise so as to make that clear.


#8

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