Steve's information gets logged twice


#1

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

My code logs steve's information twice.


var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "08978675",
        address: ['One Microsoft','Redmond','WA','98052']
    },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "8078968",
        address: ['Apple','California','UA','78695']
    }
};

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

Can someone help me in identifying the error?


#2

i think the lesson calls the function for validation, causing it to log twice


#3

{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '8078968',
  address: [ 'Apple', 'California', 'UA', '78695' ] }
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '8078968',
  address: [ 'Apple', 'California', 'UA', '78695' ] }

{ firstName: 'Bill',
  lastName: 'Gates',
  number: '08978675',
  address: [ 'One Microsoft', 'Redmond', 'WA', '98052' ] }

If that's the case then bill's information should have been logged twice.


#4

the first log of steve is caused by this in the checking script:

try {
  search("Steve");
} catch(ReferenceError) {
  return "Did you create a function called search?";
}

to validate to create a function called search, but as you can see, a function call is done.

and then there are two more function calls:

var steve_search = search("Steve");
var bill_search = search("Bill");

of course all this function calls trigger the console.log:

console.log(friends[key]);

to log something to the console


#5

Okay. Now that makes sense. Thank you.


#6