Contact list 6. List 'em all!


#1

What is wrong with my code?

var friends = {
name: 'Clarissa',
phone: '555-555-5555'
};
var friends = {
bill:{
firstName:"Bill",
lastName:"Billy",
number: "555-555-5555",
address:['111 jdnaj street'],
},
steve:{
firstName:"Steve",
lastName:"Stevey",
number: "555-555-5555",
address:['23232 kjfkd street'],
}
};
var list = function(friends) {
for(var key in friends){
console.log(list)
};
};


#2

the for loop in the hint isn't meant to be copied and pasted. The 'var key in friends' part is supposed to be replaced with code that makes the for loop work


#3

Also, the last key in an object does not require a comma. Remove the commas after both address keys. Lastly, console.log();s should always end with a semi-colon. :slightly_smiling:


#4

I'm currently on the same unit and may be wrong, but let me try to break it down plainly to the best of my understanding.

Take a look the code I used (which passed the lesson):

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "6888 8899",
        address: ["1 Marina Boulevard", "Singapore", "018989"]
    },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "5678 5678",
        address: ["Apple Campus", "Cupertine", "California", "95014"]
    },
    ron: {
        firstName: "Ron",
        lastName: "Weasley",
        number: "7890 7890",
        address: ["The Burrow", "Ottery St. Catchpole", "Devon", "32445"]
    },
    sirius: {
        firstName: "Sirius",
        lastName: "Black",
        number: "3456 3456",
        address: ["12 Grimmauld Place", "Islington", "London", "237346"]
    }

};

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

The console result:

bill
steve
ron
sirius

Explanation:

Although the instructions may seem somewhat abstruse (I was a little confused), basically what we're supposed to do with the list function is to simply print a list of all the friends. Meaning print out the keys of the second-level objects (the persons) stored inside your 'friends' object.

I wrote that part like this:

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

I used "person" as my placeholder word. In general when writing code it's good to use words that are easy to understand semantically, that make sense in plain English.

What happens here is the for loop cycles through all 4 objects inside 'friends', and prints out each of the keys I assigned them. It results in this:

bill
steve
ron
sirius

Notice they are not capitalised—because it's not the firstName key that we are printing.

Further explanation of keys:
You could think of all these objects here as a hierarchy of smaller boxes nested within bigger boxes. A key is akin to a label on a box, while values are like the contents of the box.

Level 1 — I have a main object (big box) with a key of 'friends'.
Level 2 — Inside 'friends' are 4 objects (medium boxes): with the keys of "bill", "steve", "ron" and "sirius".
Level 3 — Inside each person's medium-sized box are several objects (small boxes) with keys like "firstName", "address" etc.
Level 4 — Each small box contains one or more values.

Hope this helps! If anyone can correct my understanding please go ahead :slightly_smiling:


Question 7, got it right but code seems redundant
#5

I try your code and it's work but I can't understand why person defined in loop without existing person in the friends array?


#6

Im getting trolled i think :S
Whats wrong with my code?

var list = function(friends){

for (var buddies in friends){
console.log(friends);
}

var friends = new Object();

friends.bill = new Object();
friends.bill.firstName = "Bill";
friends.bill.lastName = "Gates";
friends.bill.number = "+31-0612345678";
friends.bill.address = ['Rich ■■■■■ road','TewJown','1234','Amsterdam']

friends.steve = new Object();
friends.steve.firstName = "Steve";
friends.steve.lastName = "Goats";
friends.steve.number = "+31-0687654321";
friends.steve.address = ['Goat road','Goatvile','4321','Palestine']


#7

Hey, you just need to close your code.
At the bottom if you add } };
it should work.


#8

also some semi-colons after your array.


#9

Thanks for your feedback bro, i really appreciate that!
sadly enough the following code:

var list = function(friends){

> for (var buddies in friends){
> console.log(friends);
> }

> var friends = new Object();

> friends.bill = new Object();
> friends.bill.firstName = "Bill";
> friends.bill.lastName = "Gates";
> friends.bill.number = "+31-0612345678";
> friends.bill.address = ['Rich B*tch road','TewJown','1234','Amsterdam'];

> friends.steve = new Object();
> friends.steve.firstName = "Steve";
> friends.steve.lastName = "Goats";
> friends.steve.number = "+31-0687654321";
> friends.steve.address = ['Goat road','Goatvile','4321','Palestine'];
> };

still gives me the following output/error:

{ bill:
{ firstName: 'Bill',
lastName: 'Gates',
number: '+31-0612345678',
address: [ 'Rich ■■■■■ road', 'TewJown', '1234', 'Amsterdam' ] },
steve:
{ firstName: 'Steve',
lastName: 'Goats',
number: '+31-0687654321',
address: [ 'Goat road', 'Goatvile', '4321', 'Palestine' ] } }
{ bill:
{ firstName: 'Bill',
lastName: 'Gates',
number: '+31-0612345678',
address: [ 'Rich ■■■■■ road', 'TewJown', '1234', 'Amsterdam' ] },
steve:
{ firstName: 'Steve',
lastName: 'Goats',
number: '+31-0687654321',
address: [ 'Goat road', 'Goatvile', '4321', 'Palestine' ] } }

> Oops, try again. It looks like your list function isn't printing the right output.

#10

Just learning this stuff myself so if I explain something wrong please point it out. I struggled with this problem (and still do) because it is a hard concept for myself to grasp.

When writing the for/in loop, make sure you use the variable name and not the main object name when you console.log.

Like this,
for (var something in friends) {
console.log(something);
}


#11

Thanks for your contribution Javaslayer, sadly enough i used a loop of this format already(see my post of 5d ago):

var list = function(friends){

for (var buddies in friends){
console.log(friends);
}


#12

"friends" is your object.
"Buddies" is your property.
Try changing the " console.log(friends); " to "console.log(buddies);" I was also wrong about needing semi-colons after your array. It isn't necessary.


#13

Thanks a ton, that did the trick, i already thought that just perhaps i had to try one of those values but for some reason i didnt try!

For all of you struggling and/or reading along wondering the answer.
My final code turned out to be this:

var friends = new Object();

friends.bill = new Object();
friends.bill.firstName = "Bill";
friends.bill.lastName = "Gates";
friends.bill.number = "+31-0612345678";
friends.bill.address = ['Rich road','TewJown','1234','Amsterdam']

friends.steve = new Object();
friends.steve.firstName = "Steve";
friends.steve.lastName = "Goats";
friends.steve.number = "+31-0687654321";
friends.steve.address = ['Goat road','Goatvile','4321','Palestine']

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

Now i still dont really understand why i get this output:

bill
steve
["Goat road","Goatvile","4321","Palestine"]
I mean i called for the names to be logged to console, but why does it dump out all the vars of the second/last person in the list???


#14

Abstruse is a great word to use to describe the tasks in Codecademy. I often don't get what they are asking me to do.