6. we made a new friend


#1

WHATS WRONG WITH MY CODE

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]);
}
}
list();

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

function add(firstName, lastName, email, phoneNumber){
firstName = "joy",
lastName = "fak",
email = "gjd@duudle.com",
phoneNumber = "8954734"
contacts[2] = add;
}


#2

This is a program so we won't be storing literal values in a function. Use the parameters to make the assignments. We can assign a new object to the end of the array by using the length as the new index.

function add(firstName, lastName, email, phoneNumber) {
    contacts[contacts.length] = {
        firstName: firstName,
        // and so on.
    };
}

#4

ALSO TRIED WITH THIS AND GIVES SAME ANSWER

function add(firstName, lastName, email, phoneNumber) {
contacts[contacts.length] = {
firstName: "joy",
lastName: "fak",
email: "osp@mail.com",
phoneNumber:"83979492"
};
}

REPLIES Oops, try again. It looks like there are still only two items in the contacts array.


#5

Why are you setting literals in the function. Use the parameters as shown in my example. The literals will go into the function call to add().


#7

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