-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontactList.js
More file actions
64 lines (56 loc) · 1.89 KB
/
contactList.js
File metadata and controls
64 lines (56 loc) · 1.89 KB
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
// Array to hold the contacts
const contactList = [];
// Function to add a contact to the list
function addContact(name, phone) {
if (name.trim() === '' || phone.trim() === '') {
alert('Name and phone number cannot be empty.');
} else {
contactList.push({ name, phone });
alert(`Contact added: ${name} (${phone})`);
}
}
// Function to display all contacts
function displayContacts() {
if (contactList.length === 0) {
alert('Your contact list is empty.');
} else {
alert('Contact List:\n' + contactList.map((contact, index) => `${index + 1}. ${contact.name} - ${contact.phone}`).join('\n'));
}
}
// Function to search for a contact by name
function searchContact(name) {
const foundContacts = contactList.filter(contact => contact.name.toLowerCase().includes(name.toLowerCase()));
if (foundContacts.length === 0) {
alert('No contacts found with that name.');
} else {
alert('Search Results:\n' + foundContacts.map((contact, index) => `${index + 1}. ${contact.name} - ${contact.phone}`).join('\n'));
}
}
// Function to handle user input
function handleUserInput() {
let userAction;
do {
userAction = prompt('Choose an action: \n1. Add Contact \n2. View Contacts \n3. Search Contact \n4. Exit');
switch (userAction) {
case '1':
const name = prompt('Enter the contact name:');
const phone = prompt('Enter the contact phone number:');
addContact(name, phone);
break;
case '2':
displayContacts();
break;
case '3':
const searchName = prompt('Enter the name to search for:');
searchContact(searchName);
break;
case '4':
alert('Exiting the application.');
break;
default:
alert('Invalid choice. Please enter a number between 1 and 4.');
}
} while (userAction !== '4');
}
// Call the function to handle user input
handleUserInput();