What is the issue in my code?


#1


var friends=new Object();
friends.bill= new Object();
friends.steve=new Object();
friends.bill.firstName="Bill";
friends.bill.lastName="Gates";
friends.bill.number="222222";
friends.bill.address=['One Microsoft Way','Redmond','WA','98052']
friends.steve.firstName="Steve";
friends.steve.lastName="Jobs";
friends.steve.number="221222";
friends.steve.address=['One Apple Way','Redmond','WA','98052']

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


#2

the problem is here:

if(friends.key.firstname===name)

due to the loop, key contains a string, then the dot notation (.) doesn't work. you will have to use square brackets:

friends[key].firstname

#3

After changes, Its still throwing error:your Search function doesn't return contact info for steve.
My code after changes

var friends=new Object();
friends.bill= new Object();
friends.steve=new Object();
friends.bill.firstName="Bill";
friends.bill.lastName="Gates";
friends.bill.number="222222";
friends.bill.address=['One Microsoft Way','Redmond','WA','98052']
friends.steve.firstName="Steve";
friends.steve.lastName="Jobs";
friends.steve.number="221222";
friends.steve.address=['One Apple Way','Redmond','WA','98052']

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

#4

Okay.. I was using firstname instead of firstName
Any way code changes are below:-

var friends=new Object();
friends.bill= new Object();
friends.steve=new Object();
friends.bill.firstName="Bill";
friends.bill.lastName="Gates";
friends.bill.number="222222";
friends.bill.address=['One Microsoft Way','Redmond','WA','98052']
friends.steve.firstName="Steve";
friends.steve.lastName="Jobs";
friends.steve.number="221222";
friends.steve.address=['One Apple Way','Redmond','WA','98052']

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

But Output comes like this:-
Steve
Steve
Bill
["One Apple Way","Redmond","WA","98052"]

Please help me.


#5

from the instructions:

it should log that friend's contact information to the console and return it.

you return very specific information (address) not all information about this particular friend


#6

@stetim94 Thanks for your patience. One last question.
When I am calllling function i.e. search("Steve");
Output is fine.
Steve
["One Apple Way","Redmond","WA","98052"]

But when I am NOT calling the function, just running the above code, the output is
Steve
Steve
Bill
["One Apple Way","Redmond","WA","98052"]

from where does these output are coming randomly.


#7

they come from the checking script which validates your code, sometimes this causes prints.

nothing to worry about


#8

@stetim94 thanks a lot buddy. :slight_smile:


#9