Duplicate printout


#1

I am trying to figure out what I am doing incorrectly that is causing the code to print twice.

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

Here is the error message:
Oops, try again. It looks like your list function isn't printing the right output.

var friends = new Object();
friends.bill = new Object();
friends.bill.firstName = 'Bill';
friends.bill.lastName = 'Gates';
friends.bill.number = '206-555-5555';
friends.bill.address = ['One Microsoft Way','Redmond','WA','98052'];
friends.steve = new Object();
friends.steve.firstName = 'Steve';
friends.steve.lastName = 'Jobs';
friends.steve.number = '413-555-5555';
friends.steve.address = ['1 Infinite Loop','Cupertino','CA','95014'];
friends.patrick = new Object();
friends.patrick.firstName =  'Patrick';
friends.patrick.lastName = 'Moore';
friends.patrick.number = '240-555-5555';
friends.patrick.address = ['3254 Trafalgar Square','Waldorf','MD','20842'];

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

#2

The full output of your of your code is:

{ firstName: 'Bill',
  lastName: 'Gates',
  number: '206-555-5555',
  address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] }
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '413-555-5555',
  address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{ firstName: 'Patrick',
  lastName: 'Moore',
  number: '240-555-5555',
  address: [ '3254 Trafalgar Square', 'Waldorf', 'MD', '20842' ] }

(formatting may vary between js interpreters)

Anything else would be printed by something else and you can ignore it.
If you want a clear indicator of where your output really ends then you could add another print statement at the end of your code saying END OF OUTPUT or some such.

So next up is considering that error message and comparing what you print to what is described should be printed


#3

Thank you for pointing that out, ionatan.
I removed the array from friends[i], so the code now looks like this:

var friends = new Object();
friends.bill = new Object();
friends.bill.firstName = 'Bill';
friends.bill.lastName = 'Gates';
friends.bill.number = '206-555-5555';
friends.bill.address = ['One Microsoft Way','Redmond','WA','98052'];
friends.steve = new Object();
friends.steve.firstName = 'Steve';
friends.steve.lastName = 'Jobs';
friends.steve.number = '413-555-5555';
friends.steve.address = ['1 Infinite Loop','Cupertino','CA','95014'];
friends.patrick = new Object();
friends.patrick.firstName =  'Patrick';
friends.patrick.lastName = 'Moore';
friends.patrick.number = '240-555-5555';
friends.patrick.address = ['3254 Trafalgar Square','Waldorf','MD','20842'];

var list = function (friends) {
    for (var i in friends) {
        console.log(i);
    };
};
list(friends);
console.log('end of output');

The result is that the list of names now prints twice. However, I receive the "Way to go! [Start Next Lesson]" message. Maybe I an reading too much into this, but I am thinking the list is only supposed to print out exactly once.

  • Quick edit * I added the line: console.log('end of output'); to the end of the code, and that 'end of output' line ended up between the first list of names and the second list of names; I am also amending the above code to include this line.

#4

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