-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
152 lines (120 loc) · 4.81 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// process thoughts
//
// creating the array of contacts with five names
let contacts = [
{ id: 1, name: "James Boss", email: "[email protected]", phone: "123-456-7890" },
{ id: 2, name: "Oga Mayor", email: "[email protected]", phone: "567-644-3333" },
{ id: 3, name: "General Sugar", email: "[email protected]", phone: "555-000-4567" },
{ id: 4, name: "John Frontend", email: "[email protected]", phone: "234-111-0000" },
{ id: 5, name: "Sule Ibrahim", email: "[email protected]", phone: "234-252-3890" }
];
display_contacts();
// Function to add a new contact
function addContact() {
const nameInput = document.getElementById('name_id');
const emailInput = document.getElementById('email_id');
const phoneInput = document.getElementById('phone_id');
const name = nameInput.value;
const email = emailInput.value;
const phone = phoneInput.value;
// Performing the Validation of inputs
if (!name || !email || !phone) {
alert('Please fill in all fields.');
return;
}
// Clear input fields
nameInput.value = '';
emailInput.value = '';
phoneInput.value = '';
const newContact = { id: generateId(), name, email, phone };
contacts.push(newContact);
displayContacts();
alert('Contact added successfully!');
}
// Function to display contacts
function displayContacts() {
const contactList = document.getElementById('contactList');
contactList.innerHTML = '';
contacts.forEach(contact => {
const listItem = document.createElement('li');
listItem.textContent = contact.name + ' | ' + contact.email + ' | ' + contact.phone;
contactList.appendChild(listItem);
});
}
// Logic to search contacts
function searchContacts() {
const searchInput = document.getElementById('searchInputId').value.toLowerCase();
const filteredContacts = contacts.filter(contact =>
contact.name.toLowerCase().includes(searchInput) ||
contact.email.toLowerCase().includes(searchInput) ||
contact.phone.includes(searchInput)
);
displayFilteredContacts(filteredContacts);
}
// Function to display filtered contacts
function displayFilteredContacts(filteredContacts) {
const contactList = document.getElementById('contactListId');
contactList.innerHTML = '';
filteredContacts.forEach(contact => {
const listItem = document.createElement('li');
listItem.textContent = contact.name + ' | ' + contact.email + ' | ' + contact.phone;
contactList.appendChild(listItem);
});
}
// Function to edit a contact
function editContact(contactId) {
const contactToEdit = contacts.find(contact => contact.id === contactId);
if (!contactToEdit) {
alert('Contact not found.');
return;
}
const newName = prompt('Enter the new name:', contactToEdit.name);
const newEmail = prompt('Enter the new email:', contactToEdit.email);
const newPhone = prompt('Enter the new phone number:', contactToEdit.phone);
if (newName && newEmail && newPhone) {
contactToEdit.name = newName;
contactToEdit.email = newEmail;
contactToEdit.phone = newPhone;
displayContacts();
}
}
// Function to delete a contact
function deleteContact(contactId) {
const contactIndex = contacts.findIndex(contact => contact.id === contactId);
if (contactIndex === -1) {
alert('Contact not found.');
return;
}
contacts.splice(contactIndex, 1);
displayContacts();
}
// Function to generate a unique ID for new contacts
function generateId() {
return Math.max(...contacts.map(contact => contact.id), 0) + 1;
}
displayContacts();
function showAllContacts() {
const allContacts = [
{ id: 1, name: "James Boss", email: "[email protected]", phone: "123-456-7890" },
{ id: 2, name: "Oga Mayor", email: "[email protected]", phone: "567-644-3333" },
{ id: 3, name: "General Sugar", email: "[email protected]", phone: "555-000-4567" },
{ id: 4, name: "John Frontend", email: "[email protected]", phone: "234-111-0000" },
{ id: 5, name: "Sule Ibrahim", email: "[email protected]", phone: "234-252-3890" }
];
displayContacts(allContacts);
}
function displayContacts(contacts) {
const contactList = document.getElementById('contactListId');
contactList.innerHTML = '';
contacts.forEach(contact => {
const listItem = document.createElement('li');
listItem.innerHTML = `
<span>" + contact.name + " - " + contact.email + " - " + contact.phone + "</span>
<div class="contact-actions">
<button onclick="editContact(${contact.id})">Edit</button>
<button onclick="deleteContact(${contact.id})">Delete</button>
</div>
`;
contactList.appendChild(listItem);
});
}