6. List em All


#1

EDIT: added change code. Now I get steve twice?

var friends = new Object();

friends.bill = new Object();
friends.bill.firstName = "Bill";
friends.bill.lastName = "Gates";
friends.bill.number= "(206) 555-5555";
friends.bill.address = ["234 Windows Way", "Someplace, CA", "99999"];

friends.steve = new Object();
friends.steve.firstName = "Steve";
friends.steve.lastName = "Jobs";
friends.steve.number = "(206) 555-5555";
friends.steve.address = ["234 iStreet", "Someplace, CA", "97899"];

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

list(friends);

#2

Delete your semicolon after the for loop. The console.log statement needs to be in the for loop's body, currently it is not.


#3

Awesome that did it! Is there a reason why it prints out steves address as well?


#4

I copied and pasted your code (and removed the semicolon) and did not get steve's address, did you update your code anymore?


#5

All I did was deleted the semicolon. Did you test in a browser console or the codeacademy console? Here is the code that I passed with.

var friends = new Object();

friends.bill = new Object();
friends.steve = new Object();

friends.bill.firstName = "Bill";
friends.bill.lastName = "Gates";
friends.bill.number= "(206) 555-5555";
friends.bill.address = ["234 Windows Way", "Someplace, CA", "99999"];

friends.steve.firstName = "Steve";
friends.steve.lastName = "Jobs";
friends.steve.number = "(206) 555-5555";
friends.steve.address = ["234  iStreet", "Someplace, CA", "97899"];

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

list(friends);

I now get bill and steve twice in code academy, but only get each name once in the console that is in developer tools.


#6

That is because codecademy tests your code to make sure it is actually right, so they probably called your function just like you did at the bottom. If you remove your list(friends); call at the bottom, you will probably still get an output and that's just from codecademy internally, nothing wrong with your code.


#7

Gotcha! Thanks for the help!