I need explanations please


#1


i copied this code from the other thread but nobody seemed to give an explanation why it doesnt work.
Another thing is how do i add an object into an array?

var contacts = [];

function printPerson (person) {
    console.log(person.firstName + " " + person.lastName);
}

function list() {
    var len = contacts.length;

    for(i=0 ;i < len;i++) {
        printPerson(contacts[i]);
    }

}

function search(lastName) {
        var len = contacts.length;

    for(i=0 ;i < len;i++) {
        if(contacts[i].lastName === lastName)
            printPerson(contacts[i]);
    }
}

function add(firstName, lastName, email, phoneNumber) {
var person = new Object();
        person.firstName = firstName;
        person.lastName = lastName;
        person.email = email;
        person.phoneNumber = phoneNumber;

contacts[contacts.length] = person;
};
add("First","Last","Email","Phone");


#2

Do you want to add it to the end of the array or to a particular position in the array?


#3

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