Contact List - 6. List 'em all! - Explanation


#1


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


This code passes but I don't think its right and if it is I don't understand it. Specifically the function at the end. It returns both first names but it doesn't make sense. Please could someone help me understand?

Thank you.


var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "0800 123456",
        address: ["One Micrsoft Way", "NY", "USA", "55587"]
    },
    
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "0800 654321",
        address: ["One Apple Way", "NY", "USA", "44867"]
    }
};

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


#2

Ok, so I think I've worked this out.

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

This returns the required output and makes sense to me and passes. However I think I should call the function but inside codeacademy it logs the names twice. Outside (e.g. jsbin.com) it only logs it with the function call e.g.

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

list(friends);

#3

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

Take a look at your for loop. Something's missing, and it makes it so your loop can't run.


#4

That's true, but the exercise doesn't seem to want the loop to iterate through the entire 'friends Object'.


#5

It says it wants to output everyone in the friends object and my code now does and passes but I still don't know whats seemmetor says is missing

`
var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "0800 123456",
address: ["One Micrsoft Way", "NY", "USA", "55587"]
},

steve: {
    firstName: "Steve",
    lastName: "Jobs",
    number: "0800 654321",
    address: ["One Apple Way", "NY", "USA", "44867"]
}

};

var list = function(obj) {
for(var prop in obj)
console.log(prop);
};

`


#6

curly braces in the for loop


#7

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