7. Search for a friend


#1


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


Hello there,

It says that my code is correct but the output looks funny too me. I'm getting Steve printed twice, Bill printed once and Steve's address once again... Can somebody who understands javaScript explain to me what is going on here?

Thank you very much for your time and help! :slight_smile:


var friends= new Object();

friends.bill= new Object();
friends.bill.firstName= "Bill";
friends.bill.lastName= "Gates";
friends.bill.number= "987 987 987";
friends.bill.address= ["microsoft", 17];

friends.steve= new Object();
friends.steve.firstName= "Steve";
friends.steve.lastName= "Smith";
friends.steve.number= "123 123 123";
friends.steve.address= ["apple", "iphone", 7];
var list= function(friends){
    for (var prop in friends)
    {
        console.log(prop);
    }
};    
var search= function(name) {
    for (var key in friends){
        if(name===friends[key].firstName)       {
            console.log(friends[key]);
            return friends[key];
        }
    }
};


#2

this has very little to do with Js, the exercise calls your function (behind the scene) to validate your function/code works correctly, but this also causes output


#3

You created friends object and put there two new objects. So, in the first par you initialized object which contains two nested object bill and steve.

Next you have function expression list() which takes object as a parameter. This function logs properties. So

list(friends);

returns bill and steve.

Next function logs all properties of nested object if parameter name matches with property firstName and returns nested object. So

search("Steve");

logs all properties of steve object.


#4

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