6. We made a friend


#1

ar 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 function
then call it passing "Jones"*/
var search = function(lastName) {
var contactsLength = contacts.length
    for (i=0; i<contactsLength; i++){
    if (contacts[i].lastName === lastName) {
        printPerson(contacts[i])
    };
    };
};

var add = function (firstName, lastName, phoneNumber, email) {
  var contact = {
    firstName: firstName,
    lastName: lastName,
    phoneNumber: phoneNumber,
    email: email
  };
 contacts[contacts.length] = contact;
 };
 add("Ariana", "Trafford", "555-555-5555", "arianatrafford@email.com")

list()


My code works fine.... I am very confused on one line tho and am hoping someone can explain it to me.  Not sure if I forgot it or if it wasn't even covered.

What is the purpose of:

contacts[contacts.length] = contact;


#2

What is the intention behind this topic?
If you're looking for help with something, then please make it abundantly clear what you want to know.


#3

I am asking what is the purpose of the line:
contacts[contacts.length] = contact;


#4

See how that's not visible without scrolling to the bottom of your code box? That's a problem!

That line stores a contact at the last index plus 1 in contacts


#5

Sorry I just assumed you would want to see the code in its entirety.

Thank you for your reply