8. Modifying My Contact List


#1

I added an else statement to my function to my loop but when I enter a name into my search function (that should bring up a result) the function automatically prints out the else function rather than the if one that it fits. When I take out the else function it gives me the proper response though. Help!

Here is my code:

var friends = {
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "507-555-5555",
address: ['1 Infinite Loop','Cupertino','CA','55952']
},
bill: {
firstName: "Bill",
lastName: "Gates",
number: "459-444-4444",
address: ['1 Microsoft Way','Redmond','WA','98052']
},
shari: {
firstName: "Shari",
lastName: "Johnson",
number: "507-459-5389",
address: ['288 W Broadway','Leithold','WI','66082']
},
brooke: {
firstName: "Brooke",
lastName: "Johnson",
number: "507-459-2839",
address: ['288 W Broadway','Leithold','WI','66082']
}
};

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

var search = function(name) {
for(var key in friends) {
if(friends[key].firstName === name) {
console.log("Name: " + friends[key].firstName + " " + friends[key].lastName);
console.log("Number: " + friends[key].number);
console.log("Address: " + friends[key].address[0]);
console.log(friends[key].address[1] + ", " + friends[key].address[2] + " " + friends[key].address[3]);
}
else {
return("That person is not in your address book. Make sure you enter the first name only.");
}
}
};

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

Rather than returning Bill's contact info it returns my else function.

Thanks!


#2

Hi @datarunner73465
You don't need the else condition because when the if condition won't be true, he will return false. Try this :

var search = function(name){
    for(var key in friends){
        if(friends[key].firstName === name) {
            console.log("Name: " + friends[key].firstName + " " + friends[key].lastName);
            console.log("Number: " + friends[key].number);
            console.log("Address: " + friends[key].address[0]);
            console.log(friends[key].address[1] + ", " + friends[key].address[2] + " " + friends[key].address[3]);
        }
}
};

Hold on :wink:


#3

var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};

var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person)
{
console.log(person.firstName + " " + person.lastName);
};

function list() {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++)
{
printPerson(contacts[i]);
}
};
/*Create a search functionthen call it passing "Jones"*/

function search (lastName)
{
var contactsLength = contacts.length;
for (var i= 0; i< contactsLength; i++)
{
if (contacts[i].lastName === lastName)
{
printPerson(contacts[i]);
}
}
};

var add = function(firstName, lastName,phoneNumber,email){
contacts[contacts.length] = {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.email = email;
}
};

add("ahmed","khattab","00201114845421","ahmed@hotmail.com");
list();

error: Oops, try again. There was a problem with your syntax.
and: SyntaxError: Unexpected token .

i also tried like this:

var add = function(firstName, lastName,phoneNumber,email){
contacts[contacts.length] = {
this.firstName : firstName,
this.lastName : lastName,
this.phoneNumber : phoneNumber,
this.email : email
}
};

thnaks in advance