7. It won't return steve contact information


#1

var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: [ "One Microsoft Way", "Redmont", "WA", "585849"]
};
friends.steve = {
firstName:"Steve",
lastName:"Jobs",
number:"(402) 555-5555",
address:["1 Infinite loop","Cupertino","CA","95014"]
};

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

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


#2

Hey @brian_marmon,

You said "var name in friends" here, right?:smiley:

So you have state [name] here:

:smiley:


#3

hey @ragezapper
I tried to do what I thought you mean but it tells me that I need to create a search function then


#4

Did you do it like this?

if(friends[name].firstName === name){


#5

Hey, I have the same problem. When i do it like ragezapper says, it tells me to create the search function.


#6

@pete202
Try and use the first example in the unit. If you want try and post it here and I can try and figure it out to help you :smile:


#7

var friends =
{
bill:
{
firstName: "Bill",
lastName: "Jobs",
number: "(677)-432-0475",
address: ['Two uM Way','Richmond','VA','44593']
},
steve:
{
firstName: "Steve",
lastName: "Gates",
number: "(356)-752-3395",
address: ['One kM Drive','Gainsville','IL','45067']
}
};

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

var search = function(name)
{
for(var key in friends)
{
if(friends[name].firstname === name)
{
console.log(friends[name]);
return friends[name];
}
}
};
search("steve");
search("bill");


#8

@pete202
I replaced a few things that helped my code work but it seems to not return the information for steve so my best advice is to make an article. Though do that after trying to compare it to the first lesson in this unit, since that helps me. Here is what I tweaked :
var friends =
{
bill:
{
firstName: "Bill",
lastName: "Jobs",
number: "(677)-432-0475",
address: ['Two uM Way','Richmond','VA','44593']
},
steve:
{
firstName: "Steve",
lastName: "Gates",
number: "(356)-752-3395",
address: ['One kM Drive','Gainsville','IL','45067']
}
};

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);
search("Steve");


#9

Thanks, @brian_marmon, I'll see what I can do with this.


#10

var friends = new Object();

friends.bill = new Object();
friends.steve = new Object();

friends.bill.firstName = "Bill"
friends.bill.lastName = "the butcher"
friends.bill.number = "556"
friends.bill.address = ['A shopping market isle','Meat produce','Chicken']
friends.steve.firstName = "Steve"
friends.steve.lastName = "the Hobo"
friends.steve.number = "667"
friends.steve.address = ['Restaurant','toilets','male']
var list = function(friends){
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]);
return friends[key];
}
}
};
Here is a code.
See whether you can spot any difference between ours and and see what to change.
Check the last few parts only.


#11

Is friends[name].firstName === name) different from (friends.name.firstName === name)

I think object properties or methods can be defined by either using fullstops or square brackets??


#12

I believe that they're the same, yes. :smile:


#13

idk what the problem with my code is then. Kindly stop by here


#14

try to rename steve to Steve and bill to Bill it will work