Different outcome for the same code in "Contact List" task 1 and 7


#1



7.Contact list


Hello.Why if i copy all the code from the 1.task to the 7 (Contact list,Seatch for a friend) i get different output?
If i use in task 7 the following code:


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

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

var search = function(name) {
  for(var prop in friends) {
    if(friends[prop].firstName === name) {
      console.log(friends[prop]);
      return friends[prop];
    }
  }
};

list(friends);
search("Steve");

//i get the following output:


    bill
steve
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(408) 555-5555',
  address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(408) 555-5555',
  address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(408) 555-5555',
  address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{ firstName: 'Bill',
  lastName: 'Gates',
  number: '(206) 555-5555',
  address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] }
{"firstName":"Steve","lastName":"Jobs","number":"(408) 555-5555","address":["1 Infinite Loop","Cupertino","CA","95014"]}

Why all the contact data is printed several times? The same code used in first task (Contact list, what you'll be building) get different result.
I cant understand why. Sorry for english misstakes, my english isnot very good.


#2

It is displayed multiple times because Codecademy is testing that your code is correct, by printing it out, and comparing it with the correct answers. :slight_smile: