6. List 'em all!


#1

https://www.codecademy.com/en/courses/javascript-beginner-en-3bmfN/0/6?curriculum_id=506324b3a7dffd00020bf661#

The instructions for this module ask to print out "bill" and "steve". However, the interpreter keeps displaying the details for steve. How do I modify my code so the interpreter stops printing out all the details for steve?

bill
steve
{"firstName":"Steve","lastName":"Hangy","number":"908-745-4123","address":["4810 Langley Drive","Alto Palo","CA","91820"]}


var friends = {};
friends.bill = {
    firstName: 'Bill',
    lastName: 'Madison',
    number: "123-456-7890",
    address: ['9182 Williams Rd', 'San Antonio', 'TX', '78291']
};
friends.steve = {
    firstName: 'Steve',
    lastName: 'Hangy',
    number: "908-745-4123",
    address: ['4810 Langley Drive', 'Alto Palo', 'CA', '91820']
};

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


#2

Did you call the function?

list(friends);

#3

Hey Denys,

I didn't call the function. When I added list(friends); I am getting

bill
steve
bill
steve


#4

question: Adding the function call should allow you to pass the exercise, did you pass?

The exercise also calls your function (it needs to validate your function works correctly), because your function has a console.log, this function call will also log output, don't worry about it


#5

Adding the function call did let me pass the exercise.

Okay, that makes sense. Thank you!


#6

Do you understand everything or do you still have questions?


#7

We created a function called list, which looks for the primary keys in object using the for/in loop.

It doesn't look for secondary keys (like firstName, lastName, number or address).

To display bill and steve, I needed to call the list function.

Is that right?


#8

yep, and the keys (what you call primary keys) of friends object are bill and steve. (not to confuse with "Bill" and "Steve" which are the values of firstName key)

firstName (and so on) are keys of bill and steve. Your for in loop only looks for the keys of friends (the keys of friends are bill and steve)

a function only executes when called, so yes :slight_smile:


#9

Great explanation. I also read your answer to https://discuss.codecademy.com/t/7-search-for-a-friend-its-hard-to-understand/74290/3

I have a much better understanding of how this works.

Friends (object) have 2 keys: bill and steve.

bill and steve are objects themselves, and have the keys: firstName, lastName, number, and address)

Because we are calling keys in friends object, list(friends) will display bill and steve when called on.

Thank you Stetim!


#10

Seems you understanding has improved a lot :slight_smile: Now you can move on :slight_smile:


#11

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