Search for a friend


#1

var friends = new Object();

friends.bill = new Object();
friends.bill.name = "Bill";

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way','Redmond','WA','98052']
}
}

friends.steve = new Object();
friends.steve.name = "Steve";

var friends = {
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "(206) 555-5555",
address: ['1 Infinite Loop','Cupertino','CA','95014']
}
}

var list = function(friends) {
for(var em in friends) {
console.log(em);
}
};

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

list(friends);
search("Steve");

search("Bill");

Oops, try again.
It looks like your search function doesn't return contact information for Bill.

HELP ME!!!


#2

Hi,

here is correct code:

var friends = {};

friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way', 'Redmond', 'WA', '98052']
};

friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: "(408) 555-5555",
address: ['1 Infinite Loop','Cupertino','CA','95014']
};
var list = function(obj) {
for(var prop in obj) {
console.log(prop);
}
};

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

list(friends.steve);
search("Bill");

You can compare this code with your code! Hopefully this will help you...


#3

I have a little corrected your code it was necessary remove something to make it work:

var friends = {};

friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way','Redmond','WA','98052']
};

friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: "(206) 555-5555",
address: ['1 Infinite Loop','Cupertino','CA','95014']
};

var list = function(friends) {
for(var em in friends) {
console.log(em);
}
};

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

list(friends);
search("Steve");

search("Bill");


#4

You just copied the sample code from the first lesson ...


#5

But this code works, thanks for the help


#6

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