Search for a friend


#1

var friends=new Object();
   
   friends.bill=new Object();
      friends.bill.firstName="Bill";
      friends.bill.lastName="Gate";
      friends.bill.number="3453445";
      friends.bill.address=["34","avenue","washing","340013"];
      
  friends.steve=new Object();
     friends.steve.firstName="Steve";
     friends.steve.lastName="Job";
     friends.steve.number="23322";
     friends.steve.address=["57","avenue","China","pekin","433333"];
  var list=function(friends){
      for(var key in object)
      console.log(key)
      var myString = "Hey";
      console.log();
      };
      var list = function(friends) {
for(var x in friends) {
console.log(x);
}
};
var search=function(name){
     for(var firstName in friends){
     if(friends.firstName===name){
         console.log(friends.firstName);
         return friends.firstName;}
     }};
 hi ,

the code can not pass the lesson
the response is :Oops, try again. It looks like your search function doesn't return contact information for Steve.
what is wrong with code ?thanks


Search for a friend
#2

this comparison:

if(friends.firstName===name)

isn't right.

firstName contains the properties of friends object (bill and steve object), you need to get the first name property of bill and steve, so in other words we need:

friends -> bill -> firstName
friends -> steve -> firstName

because the bill and steve changes we use a loop for that bit.


#3

var friends=new Object();

friends.bill=new Object();
friends.bill.firstName="Bill";
friends.bill.lastName="Gate";
friends.bill.number="3453445";
friends.bill.address=["34","avenue","washing","340013"];

friends.steve=new Object();
friends.steve.firstName="Steve";
friends.steve.lastName="Job";
friends.steve.number="23322";
friends.steve.address=["57","avenue","China","pekin","433333"];
var list=function(friends){
for(var key in object){
console.log(friends[key].firstName);
}
};

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

hi ,i could not pass the lesson:
it told me :Oops, try again. Did you create a function called search?

why ?i already created the var search=function(name)why it noticed me as this ?
thanks


#4

this is good:

if(friends[key]

but friends[key] will be steve and bill object, you still need to get the firstName of bill and steve and compare it with your function parameter (name)


#5

but how to correct it ?i ....


#6

well, given friends[key] will be the bill and steve object (properties of friends object), how do we get to the firstName of bill and steve object?

once you do, see if the first names equal name (the function parameter)


#7

sorry ,i donot understand ..
please give me the answer ,i can reflect it .


#8

lets say we have friends.bill, how do we get his first name ("Bill")?

giving you the answer is a violation of the guidelines, and i am personally against it as well

Programming is like math, just looking at the answer and then reflecting on it will make you fail for you exam because you didn't get to the solution yourself.


#9

ok ,left it .sorry , because i donot understand it .left it


#10

if we have an object (for example friends) we can access a property (for example bill) by doing:

friends.bill

so how would go about accessing firstName property of bill? I just showed you


#11

friends.bill.firstName?


#12

woohoo :smiley:

now we can implement this here:

if(friends[key]

[key] will be bill and steve (thanks to the loop), so how do we get to the firstName of bill and steve? :slight_smile:

now that we have the first names, we can compare them with name (your function parameter), to check if the person is in our contact list :slight_smile:


#13

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

as this ?


#14

this is good:

friends[key].firstName

but it should be in your if condition, given we want to see if friends[key].firstName equals name (your function parameter)

friends[key].firstName will give us the first names of all the properties (bill and steve and more if you added more)


#15

var search=function(friends[key].firstName){
for(var key in object){
if(friends[key]===firstName){
console.log(friends[key]);
return friends[key];
}}};
Oops, try again. There was a problem with your syntax.
while it doesnot work ......


#16

here:

var search=function(friends[key].firstName)

make your function parameter to name like you had it.

then in your if condition:

if(/* here */)

check if friends[key].firstName equals name, name will contain the first name we want to search for, wihile friends[key].firstName will go over all the first names in our object


#17

var search=function(friends){
for(var key in object){
if(friends[key]===firstName){
console.log(friends[key]);
return friends[key];
}}};
while it doesnot work still


#18

i don't understand, i literally tell you what:

your function parameter should be
what the comparison in the if condition should be

I don't understand anymore what i need to explain to help you


#19

thank you anyway ,
i will reflect it again ,it is not easy to me .
thank you


#20

ar search=function([steve,bill]){
for(var key in object){
if(friends[key]===firstName){
console.log(friends[key]);
return friends[key];
}}};

while it does not work