7. Search for a friend



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)
var search= function(name) {
    for (var key in friends){
        if(name===friends[key].firstName)       {
            return friends[key];


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


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


returns bill and steve.

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


logs all properties of steve object.


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