Contact List 6.List 'em all


#1

I've got to this point and technically it is letting me through to the next stage, but in the console log it prints steve twice and no bill! I think its something to do with the fact that i've listed var friends twice, once for bill and once for steve but I can't fix it!

This is my code:

var friends = new Object();

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "0800 555 5555",
address: ['12 Lansdowne Ave','Knightsbridge','London','W2 1RJ']
}
}
var friends = {
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "0900 777 7777",
address: ['75 Park Lane','Mayfair','London','W1 9RR']
}
}
var list = function (friends) {
for (var firstName in friends) {
}
console.log(firstName);

};
list(friends);

Any help much appreciated!


#2

hi this part

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

put the console.log inside the for loops like that

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

#3

Hi There, thanks for that tip, it is logging to the console but still saying steve twice and no bill!


#4

I don't know why its only print steve but with that structure its print all the fiststname

Spoiler Warning

var friends = {
    bill : {
        firstName: 'Bill',
        lastName: "Styles",
        number: "(207) 555-5555",
        address: ["Boulevard street","Fred","N15" ]
},
    steve : {
        firstName: 'Steve',
        lastName: "Buk",
        number: "(206) 555-5555",
        address: ["Lago santa crocce","Groov","N18"]
        }
};

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

}


#5

How funny, now I get this:

bill
steve
bill
steve!!

At least bill has appeared


#6

Now just remove the

list(friends)

#7

@nataliemp,

In some sections of this course, the course-checker Display's data
this you could then identify by placing to EXTRA code-line's:
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );


#8

Hate to be annoying everyone with this but I've read and used everything here and I still cant get it right. Anyone have any idea where I'm going wrong ?

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: 0123456789,
address: ['One Microsoft Way','Redmond','WA']
},
steve: {
firstName: "Steve",
lastName: "Jobs",
number: 9876543210,
address: ['Infinite Loop','Silicon Valley','CA']
},
var list = function(friends){
for(var firstName in friends){
console.log(firstName);
}
list(friends)
}
};


#9

Does it work if you put a semicolon after Steve's details? Also someone advised me to remove list(friends) at the end which helped my double up problem.


#10

Nope, dosnt work I just keep getting a unexpected token error


#11

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: 0123456789,
address: ['One Microsoft Way','Redmond','WA']
},

Do you need to add a second } above to close it off properly? Apologies I'm a massive novice myself so probably not much help!


#12

I'm not sure I tried and it didnt work, hopefully someone with more experience than us will read this and figure it out :slightly_smiling:


#13

Its okay I got it :smiley:

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: 0123456789,
address: ['One Microsoft Way','Redmond','WA']
},
steve: {
firstName: "Steve",
lastName: "Jobs",
number: 9876543210,
address: ['Infinite Loop','Silicon Valley','CA']
}
};

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

#14

Hello Guys! Another one here "looking for The Light"!

I got everything, except the main thing! Ha!
Seriously now. The code order is clear, got all that, and the output was printed out as It should.
But… I didn't quite understand the FOR / IN logic. For instance, why it doesn't matter what I type for the function parameter and key's value and I will always get the correct output (all my entries)?

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

In other words, why the code bellow is also correct, with the same output?

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

If nothing refers to nothing, how come we get a result in the first place?
Thanks a mil!


#15

@ruuus,

+++ contact list explained
object description
search as function
for-in loop explained
http://www.codecademy.com/forum_questions/54a616f9937676fb3d0024c7


#16

Wow! It couldn't be more clear! Really appreciate it!

Didn't realize or remember that functions's parameter is used as a local variable in that case, but Instead thought it to be a declared object! And that was the way the function and loop would be associated/applied to the main object - friends. Got it!

I was wondering how I'd apply this loop to friends first property (the object of the object) friends.bi?

Thanks!


#17

Or you can also do this...It works too..

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

}


#18

Hi there, I need help. My code is like this -

var friends = new Object();
friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: 123456789,
address: ['One Microsoft Way','Redmond','WA','98052']
};
friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: 987654321,
address: ['1 Infinite Loop','Cupertino','CA','95014']
};

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

I passed. But I don't understand why it outputs like this -

bill
steve
{"firstName":"Steve","lastName":"Jobs","number":987654321,"address":["1 Infinite Loop","Cupertino","CA","95014"]}

Why is the second object printed?

Thanks a lot!


#19

@lavincreative,
In some sections of this course, the course-checker Display's data
this you could then identify by placing two EXTRA code-line's:
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );


#20

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

This one worked for me.