We made a friend #6


#1



Can someone tell me whats wrong ? I get this error: "Oops, try again. It looks like you didn't add an person object to the end of your contacts array"

A brief explanation would be cool too ! :slight_smile: thanks in advance!


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

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]);
            }
        }
    };
search("Jones");

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

    
add("David", "King", "(650) 999-9999", "dk@example.com");
list();


#2

There is no need to add the last entry into your array
You handle that here in the add function plus calling the add function does not make it an object like the ones I we have defined earlier it just performs an operation


#3

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

This is how your code should look. I will go through a brief explanation for you:

I like to use var functionName = function() {}, but that is only my preference. It just looks neater to me.

Now, first off, we are going to go over the object. You are creating a new object, so you need to declare it as a variable using one of the two object constructor methods. I am sure you know of both of these. Here is where you went wrong with your object:

This part of your code should not be in the object itself, but should come after the object. You tried to assign it to an array right off the bat, when you need to use proper syntax when creating the object. Here is how that line should be written:

var contact = {
  //your object here!
}

Then AFTER creating our new object, you would assign it to the array, like so:

contacts[contacts.length] = contact;

What this does, is adds our object "contact" to the array "contacts" to a certain position depending on how many contacts are in the array. If there are 5 contacts in the array, it will be added to position 5. But it will not overwrite the 5th element, because arrays start from 0. So the 5th element would be in position 4.

I hope this helps!


#4

Thank you both of you guys both for solution and explanation ! I have learn a lot and understand it in a better way too ! :slight_smile: