6/6 We Made a Friend!


#1

hi guys i dont know whats going on with my code

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"*/

function search(lastName) {
    var contactsLength = contacts.length;
    for(i = 0; i< contactsLength; i++){
        if(this.lastName = lastName){
            printPerson(contacts[i]);
            };
        };
    };
    
    search("jones");
    
    var add = function (firstName, lastName, email, phoneNumber) {
        var newEntry = {
            firstName: firstName,
            lastName: lastName,
            email:email,
            phoneNumber:phoneNumber
            }
            contacts.push(newEntry)
            var contacts = [bob, mary, add.list]
        }
        add ("Pat", "Tillman", "pat.tillman@example.com", "(123)345-5435")
        list()

#2

change the variable "var newEntry" to "contacts[contacts.length]". Just like this

function add(firstName, lastName, email, phoneNumber) {

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

};


#3

Yeah you don't need the push part as you are setting its place in the array with the contacts[contacts.length] part


#4

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