Javascript codeacademy 6/6 lesson making new contact to the contacts array and then display that new contac within the array


#1

Oops, try again.
It looks like you didn't log "Mike Tyson" to the console. Did you remember to call list() after adding your new contact?


Replace this line with your 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];

var contactsLength = contacts.length;
function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);
}

function list() { 

    for ( var i = 0; i < contactsLength; i++) { 
            printPerson(contacts[i]);
    } 
};

var search = function(lastName) { 


    for (var i = 0; i < contactsLength; i ++) { 
        if (contacts[i].lastName === lastName) {

   printPerson(contacts[i]); 
        }

    }
};

var add = function(firstName, lastName, email, phoneNumber){

    contacts[contacts.length] = { 

firstName : firstName,
    lastName : lastName,
    email : email,
    phoneNumber : phoneNumber
    }
};

add("Mike", "Tyson", "g@gmail.com", 682445);

list();


#2

you call the list function after you added MIke:

add("Mike", "Tyson", "g@gmail.com", 682445);

list();

but in your list function:

function list() { 

    for ( var i = 0; i < contactsLength; i++) { 
            printPerson(contacts[i]);
    } 
};

contactsLength should update, this line:

var contactsLength = contacts.length;

should be inside the function, otherwise the length stays 2, and it will not find the third item (since it thinks there are still only 2 items)


#3

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