6. We made a friend question


#1

Hi,

In this exercise, I was able to make the code and got it working correctly:

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];

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 (var i = 0; i < contactsLength; i++) {
        if (lastName === contacts[i].lastName) {
            printPerson(contacts[i]);
        }
    }
};

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

add('Mr', 'Porter', 'mrporter@gmail.com', '12345678');
list();

I printed out the array length and it's indeed 3, so the 'add' function indeed added another entry to the array. But step 3 in the instructions says:

  1. Add this new contact object to the contacts array.

The code looks like it does the job without this, though, I tried adding it anyway as it instructs:

var contacts = [bob, mary, contacts[contacts.length]];

But this returns:

TypeError: Cannot read property 'firstName' of undefined

How am I supposed to add it to the array, and why do I need to actually? Seems like it's working without it as well.

Thx


#2

Hi,

this is not needed

var contacts = [bob, mary, contacts[contacts.length]];

Because you did your Job welll with your code here

 contacts[contacts.length] = {
        firstName: firstName,
        lastName: lastName,
        email: email,
        phoneNumber: phoneNumber
    };

Remove from var contacts = [bob, mary, contacts[contacts.length]]; this contacts[contacts.length]
and it will work fine :slight_smile:


#3

Yes I know it works fine without that, I was asking why the instructions said to add the new contact to the array, when clearly it's not needed?

:slight_smile:


#4

No it is needed :slight_smile:

Because many ways goes to rome :smiley:

Look my code:

function add (firstName, lastName, email, phoneNumber){
    var newContact = {
        firstName: firstName,
        lastName: lastName,
        email: email,
        phoneNumber: phoneNumber
    };
    //here my step Three    
contacts.push(newContact);

#5

Thanks, but I'm referring to the step in the exercise which says:

"Add this new contact object to the contacts array."

It says you actually have to add the object to the array, with this syntax:

array[array.length] = object

Not sure how to do that?


#6

Three ways to do that.

First:

function add (firstName, lastName, email, phoneNumber){
    var newContact = {
        firstName: firstName,
        lastName: lastName,
        email: email,
        phoneNumber: phoneNumber
    };
    //here my step Three    
contacts.push(newContact);

Second:

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

Third is your solution:

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

Your solution is the example in the instructions and they stand

(Assuming you defined the add function with the parameters firstName, lastName, phoneNumber, and email.)

That will automatically create a new object and add it into the array. Pretty neat.

So if you use the solution from the instructions the third point you didnt need to do.

  1. Add this new contact object to the contacts array.

#7

Got it, that makes sense! Thanks