6. List em All


#1

Hi I inputed this code and it says i Am missing the list function. What am i doing wrong;

var friends = {
location: 'California',
date: 'Friday'
};
var friends = new Object();
friends["location"] = "California";
var friends = new Object();
var friends = new Object();

friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way','Redmond','WA','98052']
};

friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: "(101) 222-3303",
address: ['One Microsoft Way','Redmond','WA','98052']
};

var list = function (friends) {
for (var friends in object) {
console.log("Friends Address Here");
var friends = "Cool";
console.log(friends);
}
};


#2

I would recommend using the key value to change this a little. Instead of for(var friend in object) use (var key in friends) Then instead of console.log friends console.log the key


#3

Try this out..

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

#4

HOW did you do that?! like how does it work?


#5

The friend parameter taking the inside values of the friends array and logging it.


#6

Thank you, your suggestion worked, I thought of that the first time but I am so dumb I did not follow up with the idea.


#7

But why does it only print the first objects ('Bill' & 'Steve') within the friends object? How does it know we only want their 'firstName'

I don't understand the 'in' or 'key' words - are they magic??

Thanks in advance :slight_smile:


#8

for...in doc can help you.


#9

escaney, I think it's returning the name of the objects and not the strings stored in the firstName properties of those objects. (ie: bill and steve and not Bill and Steve).

There might be something wrong with this one. The code I got to work was:

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Traynor",
        number: "0123456789",
        address: ["1 School Lane","Teachville","Impartland"]
        },
    steve: {        
        firstName: "Steve",
        lastName: "Lerner",
        number: "9876543210",
        address: ["2 Study Street","Gainsborough","Spongia"]
        }
    };

var list = function (input) {
    for (var friend in input) {
        console.log(friend);
    };
};

Which doesn't have a line to call the list function and so nothing is being passed as the 'input' argument so the function has no reference to the 'friends' object yet it still gave me 'bill' and 'steve' in the console.

When I add a line to call the function

list(friends)

The output is duplicated so I get 'bill' and 'steve' twice. It's like the code is executing the function before its called, and then executing it again when it is called. Both times operating on the 'friends' object.

Is this lesson faulty or am I missing something?


#10

Try your code in browser console:

Result:
list(friends);
bill
steve

without any duplicates.


#11

It would have been much easier if the [for/in] loop was explained, in an additional sentence. The lack of explanation completely killed the momentum for this whole track =/ I had to copy the code from the opening example just to understand what was being asked.

Regardless, it was marked correct though not only were "bill" and "steve" printed, but the all of the properties (name/value pairs) of the second instance of the "friends" object was printed. I found out why this is happening: calling the "list" function was not included in the instructions.

This lesson seems to contain teaching errors. It also seems that there is an error in the console display, throughout this lesson. I was interested in upgrading to Pro after this and the jQuery track, but now I'm thinking that Team Treehouse would be better...


#12

great it works well but i dont understand 'friend were it comes from


#13

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