Contact list not updating data


#1

I am stuck on this part

List 'em all!
Great work! Now let's add a couple of functions to help us go through our contacts.

The first function we'll create will be called list, and it will print out all the entries we have in our friends object. To do this, we'll want to use a bit of new syntax: a for/in loop.

It looks like this:

for (var key in object) {
// Access that key's value
// with object[key]
}
The "key" bit can be any placeholder name you like. It's sort of like when you put a placeholder parameter name in a function that takes arguments.

Instructions
Create a function list that takes a single parameter.
In the body of the function, write a for/in loop.
In the loop, use console.log to print out the key. (For example, if you only have bill and steve as entries, list should just print out "bill" and "steve".)

When I run the code it shows the last added contact instead of the chosen one. Any idea how to fix this?


//Objects---Start
var friends = new Object();

    friends.bill = new Object();
        friends.bill.firstName = 'Bill';
        friends.bill.lastName = 'Gates';
        friends.bill.number = ' 855-625-5500';
        friends.bill.address = ['One Microsoft Way','Redmond','WA','98052'];
        
    friends.steve = new Object();
        friends.steve.firstName = 'Steve';
        friends.steve.lastName = 'Jobs';
        friends.steve.number = ' 417-545-8471';
        friends.steve.address = ['One Apple away','Chicago','IL','14758'];
        
    friends.mark = new Object();
        friends.mark.firstName = 'Mark';
        friends.mark.lastName = 'Mark';
        friends.mark.number = ' 417-545-8471';
        friends.mark.address = ['One big name','New York','NY','14758'];
//Objects---End

//For In loop starts

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

//For In loop ends


#2

you have two options: give your function a parameter (you did, bill) and loop over the parameter or option 2: loop over the global friends

you combined this options. Please don't


#3

So instead of using bill as parameter I should be using friends? When I've used friends as param , I still receive same thing


#4

can i see an updated version of your code (just the list function)?


#5

var list = function(friends){
    
        for(var bill in friends){
            
        }
    ;}

 console.log(list);

#6

because you loop over friends, bill will now contain each property (bill, steve and mark) which you need to log to console (like you did)


#7

So should I change it inside console.log or change the loop instead?


#8

the console.log() needs to be placed inside your loop

You could also modify your loop, given bill is not a very logic name for all properties of friends


#9

I thought that I was taking billfrom friends object into a function parameter and then asking it to show all the data stored in that specific object


#10

no, we want to list all properties (bill, steve and mark) from our friend object


#11

I still don't get it. Tried changing the prop name as-well as changed the for - in loop information. Now its not even giving the output.

Btw. Thanks for your patience


#12

can i see your list function again? I can't see what changes you made.

do you understand the for in loop


#13

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

I know this one's non-sense and instead of param I should be using object , but still can't figure that out


#14

now we are back to where we started?

except know you have param instead of bill

log to the console whatever you use as iterator in the for loop (since the iterator will contain all properties of friends)


#15

Ok. Now I have all of them listed in the output

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

The result

{ bill: 
   { firstName: 'Bill',
     lastName: 'Gates',
     number: ' 855-625-5500',
     address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] },
  steve: 
   { firstName: 'Steve',
     lastName: 'Jobs',
     number: ' 417-545-8471',
     address: [ 'One Apple away', 'Chicago', 'IL', '14758' ] },
  mark: 
   { firstName: 'Mark',
     lastName: 'Mark',
     number: ' 417-545-8471',
     address: [ 'One big name', 'New York', 'NY', '14758' ] } }
{ bill: 
   { firstName: 'Bill',
     lastName: 'Gates',
     number: ' 855-625-5500',
     address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] },
  steve: 
   { firstName: 'Steve',
     lastName: 'Jobs',
     number: ' 417-545-8471',
     address: [ 'One Apple away', 'Chicago', 'IL', '14758' ] },
  mark: 
   { firstName: 'Mark',
     lastName: 'Mark',
     number: ' 417-545-8471',
     address: [ 'One big name', 'New York', 'NY', '14758' ] } }
{ bill: 
   { firstName: 'Bill',
     lastName: 'Gates',
     number: ' 855-625-5500',
     address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] },
  steve: 
   { firstName: 'Steve',
     lastName: 'Jobs',
     number: ' 417-545-8471',
     address: [ 'One Apple away', 'Chicago', 'IL', '14758' ] },
  mark: 
   { firstName: 'Mark',
     lastName: 'Mark',
     number: ' 417-545-8471',
     address: [ 'One big name', 'New York', 'NY', '14758' ] } }
["One big name","New York","NY","14758"]

It's OK and shows all of the contacts , but in the very end of the output there is a repeated line as you can see.
No idea why


#16

When changing the myVar

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

I get this output

bill
steve
mark
["One big name","New York","NY","14758"]

And test continues successfully


#17

that is much better :slight_smile:

you loop over friends directly, so no need to give your function a paramter


#18

How do I remove this ["One big name","New York","NY","14758"] line from the output?


#19

its console behavior, don't worry about it. If you really want to get rid of it, log an empty string to the console at the end of your code


#20

If you want, you can do this to prevent Codecademy from displaying the extra data ...

var list = function(buddies) {
  for (var key in buddies) {
    console.log(key);
  }
}
console.log("\n");

My output ...

mike
bill
steve