6. We made a friend!


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
We made a friend!

<In what way does your code behave incorrectly? Include ALL error messages.>
Oops, try again. It looks like you didn’t log “qwe rty” to the console. Did you remember to call list() after adding your new contact?

What’s wrong with my code?

``` function add(firstName,lastName,phoneNumber,email){ contacts[contacts.length] = { firstName: firstName, lastName: lastName, phoneNumber: phoneNumber, email: email } };

add(“qwe”, “rty”, “+123”, "123@gmail.com");

list();

<do not remove the three backticks above>

#2

@igorek9191,
Could you provide the full code you are using…


#3
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() {
	//var contactsLength = contacts.length;
	for (var i = 0; i < contactsLength; i++) {
		printPerson(contacts[i]);
	}
}

/*Create a search function
then call it passing "Jones"*/

function search (lastName) {
    for (var i = 0; i < contactsLength; i++) {
		if (lastName===contacts[i].lastName) {  
		    printPerson(contacts[i]);
	    } else {  }
	};
};

//search ("Johnson")

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

add("qwe", "rty", "+123", "123@gmail.com");

list();

#4

@igorek9191,
You just extended your contacts
and then in list() function you use the =old= contactsLength

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

you could use

for (var i = 0, contactsLength = contacts.length; i < contactsLength; i++ ) {