6. List 'em all: names appearing twice


#1

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

When I run my code, it prints

Steve
Bill
Steve
Bill

Where am I going wrong?

Code:

var friends = {
    steve: {
        firstName: "Steve",
        lastName: "a",
        number: "1234567890",
        address: ['street','city','zip']
    },
    bill: {
        firstName: "Bill",
        lastName: "b",
        number: "0987654321",
        address: ['street','city','zip']
    }
};

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

If I change the function to

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

it says my code works but it's still printing the names twice.


#2

hello there
I tried you code and it worked for me.
i noticed that you forgot to put in a semicolen (:wink: in the closing bracket of the list function.
try adding that in. :smile:
list(friends);
function list(object) {
for(var key in object) {
console.log(object[key].firstName);
}
};


#3

oh that wink face in the bracket there that is meant to be the semicolon which is this (:wink:


#4

var list = function(obj) { // define function with general object
for(var key in obj) { // loop over each key inside of object
console.log(key); // print out only primary level of key value (bill, steve)
}
};


#5

I think you'll find that the console itself is set up to call list(friends);

Since you're also calling it within your code, it prints to the console twice. I think if you take out your call to list(friends); it'll just print each name once.


#6

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