We Made A Friend! - 6/6


#1


https://www.codecademy.com/courses/building-an-address-book/0/6?curriculum_id=506324b3a7dffd00020bf661


My code seems to be correct, but I haven't adhered to the recommended code on the right of the editor, which is:
contacts[contacts.length] = {
firstName: firstName,
lastName: lastName,
phoneNumber: phoneNumber,
email: email
};
Does the code seem rational enough to be proceeded with?


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

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

add = function (firstName, lastName, email, phoneNumber){
    this.firstName = firstName ;
    this.lastName = lastName ;
    this.email = email ; 
    this.phoneNumber = phoneNumber ; 
    
    
} ;  
add("Steve", "Jobs", "stevejobs@example.come", "987987098")
list() ;


#2

i honestly think you found a corner case, the function should be coded in such a way, that we can add many people:

add("Steve", "Jobs", "stevejobs@example.come", "987987098")
add("Elon", "Musk", "elonmusk@example.come", "987987098")

How does your code handle this? and when you try to log your array to the console:

console.log(contacts)

you get an error. So i would recommend to modify the code


#3

Alright, thanks a ton! :slight_smile:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.