Help - really confused - "List 'em all"


#1



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


Hello. I am really confused at exercise 6 "List 'em all." First of all, am I supposed to be printing the keys "bill" and "steve", or the values of the key "First Name" which would output as "Bill" and "Steve"? Either way, I can't get my code to work. Please help. Thank you. My error message is: "Oops, try again. It looks like your list function isn't printing the right output."


var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "(206) 555-5555",
        address: ['One Microsoft Way','Redmond','WA','98052']
    },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "(123) 456-7890",
        address: ['One Apple Way','Cupertino','CA','12345']
    }
};

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

list();


#2

HI in this lesson you should print the name of the object you have in the friends object

var friend = {
   bill: {

   },
   steve: {

   } 
}

which will be steve and bill so this line

console.log(friends[firstName]);

just put firstName inside the console.log()


#3

Thanks. When I do that, my output is:

"bill
steve
bill
steve"

Any idea why it repeats "bill" and "steve" both twice?


#4

Since you used the console.log() in the function when you call it... it makes it print twice so you can remove the call function

list();

it will print only once.


#5

Thank you.

Ok... now I'm confused because I thought to run any function... you have to call it? How is it running without me calling it?

Also... how come my outputs aren't capitalized "Bill" and "Steve" as I've defined in my object?


#6

because they are not the propriety firstName
look at the for in

for (var firstName in friends)

in friends you have two object steve and bill

var friend = {
   //bill object
   bill: {

   },
   //steve object
   steve: {

   } 
}

so with the for in you ar printing the name of the object that inside friends so if you have

var friend = {
   bill: {

   },
   steve: {

   },
   other: {

   } 
}

with that for in it will print

bill
steve
other

#7

Ok thank you. If I had wanted to print, say, every last name... how would I have done that?


#8

you will do its like that

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

I change firstname to person to so that it doesn't get confuse with the propriety firstName


#9

Thank you... how come you are including list() at the end... won't that make the results show up twice?


#10

Yes you right. but (I don’t know why ) if you put the code in a sandbox you will only have one output with or wihout the call function

labs repl.it


#11

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