Does not returns contact info


#1

I am unable to understand what i am doing wrong, help!


7 Search a friend


Search function does not return value for Steve


var friends = {bill: {
firstName: 'Bill',
lastName: 'Gates',
number: '555666',
address: ['29/53 King william ' ,'dont' ,'5561']}, stop: {
firstName: 'Steve',
lastName: 'Job',
number: '777666',
address: ['70 out of space', 'west ploton' ,'5018']}
};

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


#2

given key contains a string (Thanks to the for in loop) you need to use square brackets.


#3

you should say the error message too so we can easily understand it :slight_smile:

ReferenceError: Steve is not defined

search(Steve);

Steve should be inside " "


#4

Thanks got it

correct code

var friends = {bill: {
firstName: 'Bill',
lastName: 'Gates',
number: '555666',
address: ['29/53 King william ' ,'dont' ,'5561']}, stop: {
firstName: 'Steve',
lastName: 'Job',
number: '777666',
address: ['70 out of space', 'west ploton' ,'5018']}
};

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


#5