7. Contact List


#1

var friends = {};

friends.bill = {
firstName : "Bill",
lastName : "Springsteen",
number : "(028) 90 616 253",
address : ['33 Colinbrooke Avenue', 'Belfast', 'Antrim']
};

friends.steve = {
firstName : "Steve",
lastName : "Fallon",
number : "(028) 90 611 233",
address : ['18 Upton Cottages', 'Belfast', 'Antrim']
};

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

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

#2

okay, lets reduce your function:

function search(name){
   for(var friend in friends){
      console.log(friend);
   }
}
search("Steve")

so we use a loop to loop over the objects inside the friends object, which as you can see are bill and steve. so now we have access to the details about our friends:

function search(name){
   for(var friend in friends){
      console.log(friends[friend]);
   }
}
search("Steve")

now we can specify the property we want to have, in our case first name:

function search(name){
   for(var friend in friends){
      console.log(friends[friend].firstName);
   }
}
search("Steve")

then we should check if this equals name, which is what you are searching for


#3

var friends = {};

friends.bill = {
firstName : "Bill",
lastName : "Springsteen",
number : "(028) 90 616 253",
address : ['33 Colinbrooke Avenue', 'Belfast', 'Antrim']
};

friends.steve = {
firstName : "Steve",
lastName : "Fallon",
number : "(028) 90 611 233",
address : ['18 Upton Cottages', 'Belfast', 'Antrim']
};

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

function search(name){
for(var friend in friends){
console.log(friends[friend].firstName);

}
}
search("Bill")

So here is my code, now I am returning the names of both Steve and Bill but not the person specified; Bill. Where am I going wrong.


#4

oh come, now you just copied my code. You know have access to the firstName, now you can check if this equals name (the function parameter, which contains the name you are searching for), you still have to create the if condition to check if they equal and then log and return the right information

I am helping you, not coding everything for you, huge difference


#5

You messed up your object syntax, you forgot to add your return statement in the search function, and what stetim94 meant in saying

, excuse me if I'm wrong, is that you need to write the name of the friend you are searching for wherever you see "friend".

(That's also how he knew that you'd copied his code.):smirk:


#6

yea, exactly, i showed how to access the first name of each friend, and then told him he still need to write the comparison, which he didn't do


#7

A post was split to a new topic: 7. contact list


#8

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