Can't figure out how to construct the add function to add an object to an array


I have no idea how to finish off the add function. I've spent quite a lot of time trying to do that myself but I've finally decided to get some help. What I don't get is how to get the function to add a 'non-existent' (yet) object to the contacts array. I've tried giving it a variable and then tried to locate it in the array using the variable but I don't think that's the case.

var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: ""

var mary = {
    firstName: "Mary",
    lastName: "Johnson",
    phoneNumber: "(650) 888-8888",
    email: ""

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++) {

/*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) {


function add(firstName, lastName, email, phoneNumber) {
    var contact = {
        firstName: this.firstName,
        lastName: this.lastName,
        phoneNumber: this.phoneNumber,

contacts[2] = add("Adam", "Smith", "", "(650) 999-9999");


Here do not include the this. keyword in the values,

2. You were so close, all you need to do is add all the parameters to the contacts array along with their values,

contacts[contacts.length]= contact

  1. Your last bracket needs a semi colon,

Lastly, you are to call the add function not try append a new contact using list accessing,

add("Example","ahs","","(325) 115-9048");


Oh, cool! Can I ask you why we don't need the this. keyword in this case? I thought this. is used to pass perimeters of the function further down the function.


Yes but that is when the parameters are not known meaning they can be added dynamically but the way the Address book code is et up you would have to pass all the required parameters manually. :grin:


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