If you have any query feel free to chat us!
Happy Coding! Happy Learning!
To create a contact list in JavaScript, we can use an array to store the contact objects. Each contact object can have properties such as name, email, phone number, etc.
Here is an example of how we can create a contact list:
javascriptCopy code
// Create an empty array to store the contacts
let contacts = [];
// Function to add a new contact
function addContact(name, email, phone) {
// Create a new contact object
let contact = {
name: name,
email: email,
phone: phone
};
// Add the contact to the array
contacts.push(contact);
}
// Function to display all contacts
function displayContacts() {
// Loop through the contacts array and display each contact
for (let i = 0; i < contacts.length; i++) {
console.log("Name: " + contacts[i].name);
console.log("Email: " + contacts[i].email);
console.log("Phone: " + contacts[i].phone);
console.log("--------------------------");
}
}
// Add some contacts
addContact("John Doe", "[email protected]", "555-555-5555");
addContact("Jane Doe", "[email protected]", "555-123-4567");
addContact("Bob Smith", "[email protected]", "555-987-6543");
// Display all contacts
displayContacts();
In this example, we have created an empty array called contacts
to store the contact objects. We have also created two functions, addContact
and displayContacts
.
The addContact
function takes three parameters, name
, email
, and phone
, and creates a new contact object with these properties. The function then adds the contact object to the contacts
array using the push
method.
The displayContacts
function loops through the contacts
array using a for loop and displays each contact object's properties.
We have also added some contacts using the addContact
function and displayed all the contacts using the displayContacts
function.
This is just an example, and in a real application, we might want to use a database to store the contact information or use some libraries to manage the contact list.
Comments: 2
I am not able to access videos from second class and further. I have already completed first class
When will I get my course?
Now, Your query was resolved.