Add Function -- "Did you remember to call list?""


#1
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;

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

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

//Create a search function
//then call it passing "Jones"
var search = function(lastName){
    for(var j=0; j < contactsLength; j++){
        if(lastName === contacts[j].lastName){
            printPerson(contacts[j]);
            }
        }
    }; 
    
//function to add
var add = function(firstName,lastName,email,phoneNumber){
contacts[contacts.length] = {
    firstName: firstName,
    lastName: lastName,
    email: email,
    phoneNumber: phoneNumber
    };
};

add("Satchel", "French", "sf@codecademy.com","1234");

list();

#2

Solved it! var contactsLength = contacts.length; was placed outside of the list function, so its value was never updated when listing the contents of contacts.


#3

Did you change the code in your post? Because it's inside the function in your post.
Whatever, poorly written submission test, decide for yourself if it's correct, as you should do anyway.